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: