Emacs Advent Calendar 19: Keyboard Macros
There are some tasks that are tedious to do by hand or impractical to do with substitution and regular expressions, but do not yet demand full blown programming in Emacs Lisp to solve. The reason for that are Emacs' keyboard macros.
To start recording a keyboard macro, press
F3 and do what you
want to do one time. To finish recording the macro press
F4. Repeating the last stored keyboard macro is accomplished
with another press of
F4. When numerically prefixed, the macro
is run multiple times. To apply a macro to each line in a region
C-x C-k r.
To append commands to a macro, press
C-u F3. This will first
execute the macro and append all succeeding keystrokes until the next
Each keyboard macro has a counter for how often it has been called. To
insert the value of the counter into the buffer, press
during macro definition. To set the value in the counter outside macro
C-x C-k C-c. To insert the value outside a
C-x C-k C-i.
Macros can be given a name for the current Emacs session with
C-x C-k b will let you bind the macro to a
key sequence. The sequences
C-x C-k N with N in [0-9A-Z] are
reserved for that purpose.
Full documentation on keyboard macros can be found in all sections of chapter 17 (Keyboard Macros) in the Emacs Reference Manual.Tags: emacs
This text by Ludger Sandig is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.