Emacs: Keyboard Macro
Emacs keyboard key macro (kmacro) feature lets you record and playback keystrokes. The key strokes can include calling any emacs commands.
Record a Key Macro
- To start recording, press Ctrl+x ( (
- Type your keystrokes.
- To stop recording, press Ctrl+x ) (
If you made a mistake, you can cancel Ctrl+g (
keyboard-quit) and start over.
Run Keyboard Macro
To run the keystrokes you've just recorded, use one of:
another useful command to run key macro is
If you call
apply-macro-to-region-lines make sure that when you record a macro, best to start at beginning of each line, and end at the same line. No need to move to next line.
Save Keyboard Macro
Key macro can be saved for future use.
To save the macro:
name-last-kbd-macroand give it a name.
insert-kbd-macro. This will insert the lisp code for a named kmacro at the cursor position.
- Copy and Paste the code into your Emacs Init File.
- In future emacs session, you can call your kmacro by name. e.g. Alt+x name.
You can now also give it a keyboard shortcut, such as F8. [see Emacs: Define Keybinding]
Emacs Keyboard Macro video tutorial
Turn Off line-move-visual
When you record keystrokes, you might want the arrow up/down keys move the cursor by a logical line, as opposed to visual line. For how to change that, see: Emacs: line-move-visual.