Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert a backslash?

Tags:

elisp

So I was looking for a way to save Emacs keyboard macros as elisp code - substituting the Emacs key description language that is used by 'insert-kbd-macro to the actual functions that are mapped to the keys of the macro.

In doing so, I was looking at first retrieving the functions mapped to keys, followed by rendering these functions as strings to be written into the current buffer

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

Would return the string "other-window"

However, currently insert-kbd-macro saves macros in caret notation (not the nice human-readable one) (eg: ^P vs \C-p) The function key-binding seems to only accept the human readable notation.

So in an effort to convert to human readable notation, i looked at the function key-description

(key-description "\346")

returns "M-f"

However in order to be accepted by key-binding , it requires notation in the form `"\M-f")

an obvious way to do this would be

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

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

To figure out what was going on, I decided to see what the raw byte for the character "\" is displayed as....

(byte-to-string 92)

it returns '\\'

I suspect it might be a bug in elisp.

like image 381
jones Avatar asked Mar 22 '23 21:03

jones


2 Answers

The answer is Don't insert /'s - use read-kbd-macro to get readable text that also works with key-description and key-binding.

(key-description (read-kbd-macro "M-f"))

(key-binding (read-kbd-macro "M-f"))

If you really want to insert a \, as you noticed in your comment, you can do so via

(insert "\\")

But, to save you a lot of angst, you might want to abandon this effort as it's not currently possible to translate keyboard macros into elisp in a general fashion. See the question "Convert Emacs macro into Elisp".

like image 61
Trey Jackson Avatar answered Apr 29 '23 05:04

Trey Jackson


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 the defined function

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

like image 41
guest Avatar answered Apr 29 '23 04:04

guest