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.[3]

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.

[3] 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 ...

Get Learning GNU Emacs, 3rd 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.