Emacs: Keyboard Macro
This page shows you how to use emacs keyboard macro feature.
Emacs keyboard macro (kmacro) feature lets you record and playback keystrokes. The key strokes can include calling any emacs commands.
How to Record a Sequence of Keystrokes?
- To start recording, press 【Ctrl+x (】 (Alt+x
- Type your keystrokes.
- To stop recording, press 【Ctrl+x )】 (Alt+x
If you made a mistake, you can cancel 【Ctrl+g】 (Alt+x
keyboard-quit) and start over.
Run Keyboard Macro
To run the keystrokes you've just recorded, use one of:
Most Useful Keyboard Macro Commands
kmacro-end-and-call-macro is just like
call-last-kbd-macro, except that if a kmacro recording is still on-going, it ends it first.
Note: there are more keyboard macro commands than listed above. To list them, Alt+x
apropos-command then type “macro”.
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: How to Define Keys]