Adding to an Existing Macro

You can’t edit macros, but you can add some commands to the end of a macro by typing C-u C-x (. This command executes the macro you’ve written so far and then waits for you to add more keystrokes. When you’ve finished adding keystrokes, type C-x ) to end the macro definition.

This command is especially useful when you write a macro and then realize that it isn’t quite complete. You can either type the extra keystrokes yourself each time you execute the macro or append those keystrokes to the end of the macro. You may want to append keystrokes to the macro if you realize that the macro you’ve written doesn’t set itself up to be executed again. In the preceding example of copying references to Emacs to another buffer, we originally omitted the command that moves back to the first buffer and then added it in later.

Example

For example, let’s assume that you decide you want to collect only the first occurrence of Emacs in each section of your document. The document happens to be written in troff (UNIX’s built-in typesetting system), and you can recognize sections because they begin with .NH. Let’s change the macro to add this feature.

Keystrokes

Action

C-u C-x (

Start adding to the current macro; Def appears on the mode line.

C-s .NH

We’ve found Emacs and are back to the original buffer. Now search for the beginning of the next section.

RETURN

Stop the search after it is successful.

C-x )

End the macro definition; Def is removed from ...

Get Learning GNU Emacs, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.