: Learning GNU Emacs, 3rd Edition

6.4 Editing a Macro

6.4 Editing a Macro

You can edit a macro and make changes to it in a few different ways. For this example, we chose an all-purpose editing command, edit-kbd-macro, which is bound to C-x C-k e. Several macro editing commands are available, but this one works for all types of macros, so it's good to learn.

Our macro could use a bit of tweaking. First of all, finding references to Emacs in our copy of the Emacs NEWS file is pretty lame. Perhaps we're interested in using a mouse more frequently with Emacs and would like to know about changes to that part of the interface. We'll edit the macro to search for the word mouse. We'll also modify it so it marks a paragraph rather than a sentence since a sentence doesn't really provide enough context to be helpful.

Let's start editing the macro.

Type: C-x C-k e

Emacs prompts you for the type of macro to edit.

Emacs asks you if you want to edit the last keyboard macro (C-x e), a named macro (M-x), the last 100 keystrokes as a macro, termed "lossage" (C-h l), or keys (meaning the keystrokes you bound a macro to). Yes, that's a lot of choices, and later in the chapter we describe named macros and binding macros to keys (you can experiment on your own with creating a macro from lossage). For now, just choose C-x e to edit the last keyboard macro.

Type: C-x e

Emacs opens an *Edit Macro* buffer.

Notice two fields near the top of this buffer, Command: and Key:. Right now, Command: says last-kbd-macro. If this were a named macro, the command would be the name you gave your macro. Additionally, for frequent use, you can bind your macro to a key, at which point the Key: field lists the keystrokes to execute this macro. Right now it says none because we haven't defined any keystrokes yet.

Note that Emacs inserts comments all through the macro. It's attempting to map keystrokes to commands. You do not need to update these comments or add comments if you add commands to your macro; Emacs does that itself.

To tweak our macro, we change the search string on the second line from emacs to mouse. Note that we can just press C-k to wipe out the line and type mouse. Now change M-a to M-{ and M-e to M-}. We change the buffer name from emacsrefs to mouseinfo.

We've made the edits from the previous paragraph. The screen looks like this:

A modified macro that captures information about using a mouse in Emacs.

To exit the macro editing buffer, we have to type C-c C-c and go back to our NEWS buffer. Let's do that and then execute the macro again to see what happens.

Type: C-c C-c C-x b Enter M-< M-5 F4 C-x b Enter M-<

The mouseinfo buffer shows paragraphs from our copied NEWS file that mention the mouse.

: 0.577. /Cache: 3 / 0