How to insert backslash?

So, I was looking for a way to save Emacs macros as elisp code - a submenu of the Emacs keyword description language used by 'insert-kbd-macro for the actual functions mapped to macro keys.

At the same time, I first looked for functions associated with keys, and then provided these functions in the form of strings that should be written to the current buffer

 (symbol-name (key-binding "\C-xo")) 

Will return the string "other-window"

However, insert-kbd-macro currently saves macros in carriage notation (and not in a human-friendly form) (for example: ^P vs \Cp ) The key-binding function seems to accept only humanoid notation.

Therefore, in order to convert into a human-readable notation, I looked at the key-description function

 (key-description "\346") 

returns "Mf"

However, in order to be accepted key-binding , it requires a notation in the form of `` \ Mf ")

The obvious way to do this would be

(concat "\\" (key-description "\346")

However, emacs only ever returns "\\" not "\"

To find out what is going on, I decided to see how the original byte for the character "\" displayed as ...

 (byte-to-string 92) 

it returns '\\'

I suspect this may be a bug in elisp.

+4
source share
2 answers

Answer: do not insert / use read-kbd-macro to get readable text that also works with key-description and key-binding .

 (key-description (read-kbd-macro "Mf")) (key-binding (read-kbd-macro "Mf")) 

If you really want to insert \ as you noted in your comment, you can do this via

 (insert "\\") 

But, to save you a lot of longing, you can give up this effort, since it is currently not possible to translate keyboard macros into elisp in a general way. See the Question "Converting Emacs Macros to Elisp . "

+5
source

I added this function to my .emacs:

 (defun insert-backs () "insert back-slash" (interactive) (insert "\\ ")) 

and then:

(global-set-key (kbd "M-:") 'insert-backs) ; call a specific function

means that now I combine 'Meta' and ':' to get '\'

+1
source

Source: https://habr.com/ru/post/1495403/


All Articles