How to Save an Emacs Keyboard Macro Permanently
Courtesy of gnu manual, we can permanently save a keyboard macro for future use:
https://www.gnu.org/software/emacs/manual/html_node/emacs/Save-Keyboard-Macro.html
The steps are as follows:
- perform macro (start with
f3
, then do your stuff and return to where you started, then stop recording with<f4>
) - name that macro
kmacro-name-last-macro
- insert macro code into buffer
insert-kbd-macro <RET> macro-name
I want the ability to go into a let-form and, in the spirit of REPL debugging and development, bind the X VAL
part of a (let [ ... X VAL ...])
to a def’d variable, (def X VAL)
for the length of my session. I can do it easily in 14 keystrokes, which I want to save for posterity (or just my every-day work) so I don’t have to repeat those 14 keystrokes every time, including the concluding “eval this form” and then a series of “undo” commands to get the code back to its pristine former state.
Note that there’s a good chance you are using modes on your system as you perform the commands you are recording, so these might not be fully shareable; for instance, in the command I record, I am making use of structural editing with smartparens’ sp-forward-slurp-sexp
to accurately get the next forms (using a default “end of line” would have been a very fragile solution that would break on comments or end-of-let forms). The keystrokes won’t work without the default bindings to smartparens.
The end result looks something like this, including binding it to a free-key in Clojure mode:
(fset 'tsa/clojure-letvar-to-def
(lambda (&optional arg)
"With cursor at a let-var, def it so you can proceed with repl debugging."
(interactive "p") (kmacro-exec-ring-item (quote ([40 100 101 102 32 C-right C-right 134217734 134217734 134217734 24 5 67108911 67108911] 0 "%d")) arg)))
(define-key clojure-mode-map (kbd "M-L") 'tsa/clojure-letvar-to-def)