Search code examples
emacskeyboardmacroslisp

Emacs: getting readable keyboard-macros


When using insert-kbd-macro to save a named keyboard macro I get "unreadable" Lisp code like

(fset 'ppsql
   (lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item (quote ([134217788 134217765 44 return 44 17 10 return 33 134217765 102 102 backspace 114 111 109 return 17 10 102 111 109 backspace backspace 114 111 return 33] 0 "%d")) arg)))

I'd rather have something like the following:

(fset 'move-line-down
      [?\C-a ?\C-k delete down ?\C-y return up])

IIRC I used the same method to record, name, and insert both keyboard macros: F3, F4, name-last-kbd-macro.

Is it possible to get the first macro in a readable format?


Solution

  • The keyboard macro functionality in Emacs stands of two modes: macros and kmacros. The former returns the macro in a way you like—the symbol form—, the latter provides the lambda form. So that, if you call name-last-kbd-macro you get a symbol form, if you call kmacro-name-last-macro, you get a lambda form.

    Update

    As per @marcz observation, in more recent versions of Emacs, name-last-kbd-macro is an alias for kmacro-name-last-macro, and calling insert-kbd-macro will provide the following code:

    (defalias 'move-line-down
       (kmacro "C-a C-k <down> C-y <return> <up>"))