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.
6.2 Tips for Creating Good Macros
It's easy to learn how to record and reuse your keystrokes. However, when you're starting out, you make a few mistakes: you create a macro, use it, and then find out that it doesn't do exactly what you thought. With a little care, it's easy to make your macros more useful and less vulnerable to mistakes.
Good macros work in all situations. Therefore, within a macro, you should use commands that are absolute rather than relative. For example, if you write a macro that puts a formatting string around the word the cursor is on, you want the macro to work no matter how long the word is. Therefore, you would use an absolute command such as M-f (for forward-word) rather than a few C-fs to move forward one character at a time. Similarly, commands such as C-e and C-a are good for finding the beginning or end of a line rather than moving the cursor forward or backward.
Often, macros start with a search command that brings you to the place in the file you want the macro to start. It's a good idea to type the search argument (as in C-s searchstring) rather than using the command to repeat the last search (C-s C-s). You may have changed the search string between the time you define the macro and the time you execute it, and C-s C-s remembers only what the last search string was.
It is often a good idea to add extra commands (typically C-a and C-e) that aren't strictly necessary, just to make sure that you're positioned correctly on the line. The fewer assumptions that a macro makes, the better it works. So, if a sequence of commands works correctly only if you start at the end of the line, start the macro with C-e, even if you already "know" that you want to give the command only when you're at the end of the line.
Finally, while we're reciting rules and cautions, here's one more: keep in mind that you probably want to execute macros repeatedly. With a little foresight, you'll be able to create macros that can be executed in long chains without problems.
In general, good macros have three parts:
• They find the place you want the macro to start working (often using search).
• They do the work that needs to be done on the text.
• They prepare themselves to repeat.
How can a macro prepare itself to repeat? For example, assume that you're writing a macro to delete the third column of a table. After deleting the column, the macro should position itself at the beginning of the next line (or wherever it needs to be) so you don't have to reposition the cursor before reusing it.
Here's a slightly more complex example. If you start a macro with a search, you have to make sure that the end of the macro moves the cursor past the last spot you searched for. If you don't, the macro will keep finding the same place in the file and never go on to the next occurrence of what you're searching for. As a general rule, if your macro operates on a line of text, it should end by moving to the beginning of the next line. Remember that your goal is to create a sequence of keystrokes that can be executed many times in a row, with no interruption.
6.3 A More Complicated Macro Example
Sometimes you may want to find all the references to a particular topic in a file. Table 6-2 lists steps for creating a macro that takes takes every sentence in the buffer that contains the word Emacs and copies it to another buffer. If you try this macro, you'll need to type some text about Emacs into a buffer. You can also get a test file to work with by opening the Emacs NEWS file (using C-h n), then writing it to a file (C-x C-w NEWS). This buffer is in view mode by default; change to text mode by typing M-x text-mode Enter.
Table 6-2. Steps for macro that creates a buffer of Emacs references
| Keystrokes | Action |
|---|---|
| F3 or C-x ( | Start macro definition; Def appears on the mode line. |
| C-s emacs | Find the word Emacs. |
| Enter | Stop the search after it is successful; if the search is unsuccessful, it rings the bell and stops the macro. |
| M-a | Move to the beginning of the sentence.[34] |
| C-Space | Set the mark. |
| M-e | Move to the end of the sentence. |
| M-w | Copy the sentence to the kill ring. |
| C-x b emacsrefs Enter | Move to a buffer called emacsrefs. |
| C-y | Insert the sentence. |
| Enter | Start the next sentence on a new line. |
| C-x b Enter | Move back to the original buffer. |
| F4 or C-x ) | End the macro definition; Def is removed from the mode line. |
34
M-a's definition of a "sentence" is controlled by the variable sentence-end, which is a fairly complex regular expression. By default, a sentence ends with a period, question mark, or exclamation mark, optionally followed by a quotation mark or parenthesis (including brackets or braces), and followed by two or more spaces or a newline.