A little late but it’s finally up. We’re discussing keyboard macros in this episode.
Keyboard macros are Emacs’ way of recording and playing back a series of keys. It’s a useful feature that many people don’t sufficiently use. The excellent Emacs Rocks series of screencasts has some episodes on clever macro usage to reduce the number of keys needed to perform some kind of task.
The H.264 version of the video for iOS devices is at https://archive.org/download/EmacsMovies/10-episode-keyboard_macros.mkv.
C-x )to start and stop recording macros. With prefix arguments
C-u C-u, you can append to existing macros.
C-x eto replay macros. You can do this a number of times in one shot using prefix arguments. Also, the
apply-macro-to-region-linesfunction to apply a macro to all lines in a region.
C-ichord to insert the macro counter while recording a macro. Modifying the macro counter using
qcommand to stop while playing back a macro so that you can enter some text or make minor variations.
nto name the last macro and
bto bind the last entered macro to a key. Finally,
insert-kbd-macroto actually insert a macro as elisp into your current buffer so that you can save it for future use.
F4 keys offer a convenient combination of the basic macro record and playback functions.
F3 stands for
kmacro-start-macro-or-insert-counter. When it is hit the first time, the effect is the same as
( (i.e. start recording a macro). While this is going on, if it is hit again, it will work like you hit
C-i (i.e. insert the macro counter).
F4 is bound to
kmacro-end-or-call-macro. This will, if hit while a macro is being recorded, end the recording process. Here, it is similar to
). If hit while a macro is not being recorded, it will work like
e and call the last recorded macro.
Macros are a little expansive so I took some liberties filtering out some of the features that I thought were not that useful. These are
- Editing macros.
- Interactively editing macros
- The macro ring which contains all the macros created so far.
I might do another screencast describing these but the manual page is explanatory and the things which I have covered will suffice for day to day usage.
- The emacs manual page on macros
- The wiki has a page on macros that contains a lot of useful snippets and other little goodies that can improve your experience.
- Xah Lee has a page on macros with some practical examples that he uses.
- The Emacs Rocks series of screencasts has some nice examples of macro magic.