Recording and Running Macros in Emacs

A macro is like a recording of a sequence of keystrokes−−when you run a macro, Emacs executes that key sequence as if you had typed them. To begin recording a macro, type C−x (. Then, everything you type is recorded as the macro until you stop recording by typing C−x ). After you have recorded a macro, you can play it back at any time during the Emacs session by typing C−x e. You can precede it with the universal−argument command, C−u, to specify a number of times to play it back.

To record a macro that capitalizes the first word of the current line (M−c capitalizes the word to the right of point) and then advances to the next line, type:

C−x ( C−a M−c C−n C−x )

To play the macro back 20 times, type:

C−u 20 C−x e

Macros are primary to how Emacs works−−in fact, the name Emacs is derived from `Editing MACroS', because the first version of Emacs in 1976 was actually a collection of such macros written for another text editor.

Posted on: 16/12/2009

If you want to leave a comment please Login or Register