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-kto wipe out the line and type mouse. Now change M-ato M-{and M-eto 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-cand 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.
Although our latest macro is interesting, it's not really a general purpose macro. It is a temporary solution to a one-time problem. It saves you some work, but it isn't general enough to save and use again. On the other hand, our macro to transpose names is generally useful. We'd like to use it again. We'd like to bind it to a key. But it is no longer the "latest" keyboard macro.
As we mentioned earlier, Emacs has a macro ring much like the infamous kill ring. It's useful in the case we've just described, but it's also useful because of the fragility of the macro definition process. You create a macro and make a wrong move that rings the bell, and your macro is canceled. It's fairly easy to create a macro that does nothing. Perhaps the macro that you just created was wonderful, and this new nonfunctional nothing macro has supplanted it. Again, the macro ring is the solution. To delete a macro from the ring, type C-x C-k C-d(for kmacro-delete-ring-head). This deletes the most recently defined keyboard macro.
What if you want to swap the positions of two macros? Instead, type C-x C-k C-t(for kmacro-swap-ring). This transposes macros 1 and 2.
In a more general sense, you can cycle to the previously defined macro by typing C-c C-k C-p(for kmacro-cycle-ring-previous). To move the ring the other way, type C-x C-k C-n(for kmacro-cycle-ring-next). The familiar C-pfor previous and C-nfor next bindings are appended to the general macro keyboard prefix C-x C-k.
Before we can work with the transpose names macro, we must either define it again or, if you've been working through our examples, type C-x C-k C-pto move to the previous macro.
6.6 Binding Your Macro to a Key
Binding a macro to a key is easy. The key sequences C-x C-k 0through 9and capital Athrough Zare reserved for user macro bindings. You can choose one that strikes you as mnemonic for your macro.
For example, to bind our transpose names macro to C-x C-k T, type C-x C-k b. Emacs prompts for the key binding. Type C-x C-k T Enter. Emacs confirms, Keyboard macro bound to C-x C-k T
. Binding a macro command to a key in this way works for only one session. We want to keep this macro, so read on to find out how to make this binding permanent.
6.7 Naming, Saving, and Executing Your Macros
In this section, we'll describe how to save macros so that you can use them in different editing sessions. To save a macro, bind it permanently to a key, and load it in subsequent Emacs sessions, follow these steps:
1. Define the macro, if you haven't already.
2. Type C-x C-k n(for name-last-kbd-macro). Now type a name for your macro and press Enter. A non-Emacs sounding name is best so that Emacs doesn't confuse it with one of its own commands. Once you've executed this command, Emacs remembers the macro for the rest of the editing session. To use it again, type the command M-x name (where name is the name you've chosen). Emacs treats your named macro like one of its own commands; it shows up in completion lists if you press Tabafter typing a few letters of the name.
3. If you want to save the macro definition permanently, you must insert the macro definition into a file. This could be your .emacs file or a macro file that you load through your .emacs file. Type C-x C-f filename Enterto find the file into which to insert the definition and move to the end of it by typing M->.
4. Type M-x insert-kbd-macro Enter macroname Enter. Emacs inserts Lisp code that represents your macro.
5. Add a line to .emacs make the key binding permanent. For example, if we called our macro transpose-namesand bound it to C-x C-k T, we would add this line to our .emacs file (or other macro definition file):
(global-set-key "\C-x\C-kT" 'transpose-names)
6. If you save the macro in some other file, it won't be loaded automatically. For example, let's say that you have defined a macro called transpose-namesand placed it in the file html.macs , in the directory ~/macros . Add this line to your .emacs file to load your macros automatically:
(load-file "~/macros/html.macs")
7. Save the .emacs file and, if different, the file in which you inserted your macro. Exit and restart Emacs. You can now execute this macro either by typing M-x transpose-names Enteror by pressing C-x C-k T.
6.8 Building More Complicated Macros
So far, we've covered the basics of writing, executing, and saving keyboard macros. Now let's discuss a couple of more advanced features Emacs lets you add to your macros: pausing a macro for keyboard input and inserting a query in a macro.
6.8.1 Pausing a Macro for Keyboard Input
Sometimes it's useful to pause a macro briefly so you can type something. For example, if you write a lot of letters, you could have a macro that prints out a template and then pauses for you to fill in variables (such as the date and the recipient's name). You can perform this task (and similar tasks) by inserting a recursive edit into a macro. A recursive edit is just a fancy way to say, "Stop and let me type a while, then pick up the macro where I left off."
When you're defining a macro, type C-u C-x qat the point where you want the recursive edit to occur. Emacs enters a recursive edit. (You can tell you're in a recursive edit because square brackets appear on the mode line; you'll see them in the screenshots later in this section.) Nothing you type during the recursive edit becomes a part of the macro. You can type whatever you want to and then press C-M-cto exit the recursive edit. Notice how the square brackets disappear when you type C-M-c. When the square brackets are no longer on the screen, you have left the recursive edit. Anything you type at this point becomes part of the macro. You can put as many pauses in your macros as you want to.
Читать дальше