6.1 Defining a Macro
6.1 Defining a Macro
To start defining a macro, press F3 or C-x (. The abbreviation Def appears on the mode line, showing that you are in macro definition mode. In this mode, Emacs records all the keystrokes that you type, whether they are commands or literal text, so that you can replay them later. To end the macro, press F4 or C-x ); you leave macro definition mode, and Emacs stops recording your keystrokes. Emacs also stops recording your keystrokes automatically if an error occurs or if you press C-g.
While you're defining a macro, Emacs acts on your keystrokes as well as recording them: that is, anything you type while in macro definition mode is treated as a regular command and executed. While you're defining a macro, you're doing completely normal editing. That way you can see that the macro does exactly what you want it to, and you can cancel it (with C-g) if you notice that the macro isn't really quite what you want.
To execute your macro, press F4 or C-x e. Emacs then replays your keystrokes exactly. (You can see that F4 has two different functions relating to macros: to end a macro definition and, after it's defined, to execute the macro.)
This macro is referred to as the "last" keyboard macro, with last here meaning most recent. Only one macro is the last keyboard macro. A macro ring, much like the kill ring, allows you to access a number of macros during an Emacs session.
Table 6-1 shows the steps required to define and execute a macro. This macro takes a list of names in the ordinary First Name Last Name order and changes it to the frequently needed Last Name, First Name order.
Table 6-1. Steps for creating name transposition macro
|F3 orC-x (||Start the macro;
|C-a||Move to the beginning of the current line.|
|M-f||Move forward a word.|
|,||Type a comma.|
|M-t||Transpose first and last.|
|C-n||Move to the next line.|
|F4 orC-x )||End the macro definition.|
Define the macro using the keystrokes given in Table 6-1.
In defining the macro, you transposed the names on the first line, leaving the cursor on the second line.
Now let's be brave and assume the macro works; we'll try repeating it five times by prefacing the command to execute a macro with M-5. Of course, in real life, you'd be better off trying it once before doing anything so bold.
Type M-5 F4 or M-5 C-x e
Now we've done the first six lines: one by defining the macro and five more by executing it.
The macro works well, so we can finish the rest of the buffer with confidence: type M-100, then C-x e or F4. Emacs stops automatically when you reach the end of the buffer, so it doesn't matter if you repeat the macro more times than necessary.
Here are a few points to remember:
• Don't forget to press F4 or C-x ) when you've finished the macro. If you try to execute a macro before it has been defined, Emacs complains and forgets the macro's definition.
• C-g terminates a macro, causing Emacs to forget its definition.
• Virtually any error automatically terminates a macro. If Emacs beeps at you, you have to start over.
• Emacs executes the keystrokes exactly as you type them, with no intelligence whatsoever. Avoid making assumptions like, "Of course I'll be at the beginning (or end) of the line when I execute the macro."
If you invoke a macro and it does the wrong thing, you can use C-_ to undo it. Emacs is smart enough to realize that "undo the last command" means "undo the entire macro" rather than "undo the last command within the macro." However, if you repeat a macro multiple times using M-n, C-_ undoes only the last instance of the macro, not all the instances.