emacs: keyboard macros
Keyboard Macros
You can record keystrokes and commands in Emacs and save them for later playback as a keyboard macro. A keyboard macro in Emacs is very different from a lisp macro and you should not confuse the two.
Basic Commands
Key Binding | Description |
---|---|
F3 | Starts macro recording or inserts counter value |
F4 | Stop macro recording or play last macro |
C-x ( and C-x ) | Starts and stop macro recording |
C-x e | Plays last macro |
You can also pass the universal argument and digit arguments to the macro commands:
Key Binding | Description |
---|---|
C-u F3 | Starts recording but appends to the last macro |
C-u F4 | Plays the second macro in the ring |
numeric F3 | Starts recording but sets counter to numeric |
numeric F4 | Plays last macro numeric times |
Appending to the last macro (C-u F3) is occasionally useful, but passing a numeric argument to F4 is very useful since replaying the macro a set number of times is a frequent thing indeed; so much so that passing digit 0 (C-0 F4 or C-u 0 F4, for instance) will run the macro over and over again until it terminates with an error, such as reaching the end of a buffer or when a command in the macro triggers an error.
Advanced Commands
There is an entire prefix key group, C-x C-k, dedicated to Emacs’s macro functionality. There are many commands and you are unlikely to ever use most of them.
Interactive Macro Playback
Let’s start out with the counters. When you start recording, Emacs will automatically initialize an internal counter to zero, and every time you press F3 during the recording, Emacs will insert the counter and then increment the internal counter by 1. There are, of course, many creative uses for the counter: creating numbered lists is the most obvious.
Key Binding | Description |
---|---|
C-x C-k C-a | Adds to counter |
C-x C-k TAB, F3 | Inserts counter |
C-x C-k C-c | Sets counter |
C-x C-k C-f | Sets format counter |
C-x C-k q | Queries for user input while recording |
The standout command is C-x C-k q. When you call it, Emacs will tag that step in the macro recording and ask the user for advice – in effect stopping the macro temporarily to prompt the user – before continuing.
Query Key Binding | Description |
---|---|
Y | Continues as normal |
N | Skips the rest of the macro |
RET | Stops the macro entirely |
C-1 | Recenters the screen |
C-r | Enters recursive edit |
C-M-c | Exits recursive edit |
Saving and Recalling
Macros in Emacs are stored in a macro ring, a concept that you should recognize from other parts of Emacs (like the kill ring and undo ring.) Creating a new macro automatically stores old macros in the macro ring without you having to do anything. The commands below let you save and recall from the macro ring, edit and bind macros to keys, and more.
Key Binding | Description |
---|---|
C-x C-k C-n | Cycles macro ring to next |
C-x C-k C-p | Cycles macro ring to previous |
C-x C-k n | Names the last macro |
C-x C-k b | Binds the last macro to a key |
C-x C-k e | Edits last macro |
C-x C-k l | Edits the last 300 keystrokes |
M-x insert-kbd-macro | Inserts macro as elisp |