Using macros in Emacs
- Related pages
Emacs macros allows us to record sequence of keyboard keystrokes, mouse actions, menu actions that is bound to an Emacs command. It allows us to record these actions and repeat them when needed.
Creating a new macro #
C-x (
starts recording a macroC-x )
stops recording a macroC-x e
executes the macroC-u 37 C-x e
executes the macro 37 timesC-u 0 C-x e
executes the macro an infinite number of times until EOF.
Testing #
Create a macro to remove the -
from the following fruit list and replace it
with an +
(org-mode has still already setup, but for learning purpose, let’s
do it with a macro).
Place the cursor the banana
section and start recording a macro. Remove the
first -
from Banana and replace it with +
, move to the next line (apple
)
and stop recording the macro. Run C-u 0 C-x e
to run the macro until EOF.
- Banana
- Apple
- Melon
- Grape
- Orange
- Watermelon
- Kiwi
It is important to stop defining a macro at a similar position to where you
started it. Executing apply-macro-to-region-lines
applies the last-defined
keyboard macro to each of the lines of a region
Resources #
- EmacsWiki - Keyboard macros
- elmacro - Shows keyboard macros or latest interactive commands as emacs lisp
- GNU Emacs - Keyboard Macros
- Emacs keyboard macros (info)