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.
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".
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 '\'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With