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-p for previous and C-n for 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-p to 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 0 through 9 and capital A through Z are 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 Tab after 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 Enter to 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-names and 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-names and 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 Enter or 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 q at 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-c to 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.
6.8.1.1 Example
Here's an example of a macro that puts a business letter template on the screen and uses recursive edits to let you type your return address, the recipient's name and address, and the date. Because the brackets on the mode line are a pretty subtle clue to what you are going to type, we'll give the user of this macro explicit instructions about what to type. Table 6-3 provides these instructions.
Table 6-3. Steps for creating a business letter macro
| Keystrokes | Action |
|---|---|
| F3 or C-x ( | Start keyboard macro definition. |
| M-5 Enter | Put in 5 blank lines. |
| Type your address and press C-M-c | Display Type your address and press C-M-c on the screen. |
| C-a | Move to the beginning of the line. |
| C-u C-x q | Enter a recursive edit, during which the keystrokes you type are not recorded as part of the macro. |
| C-M-c | Exit the recursive edit. |
| C-e | Move to the end of the line. |
| M-5 Enter | Move the cursor down 5 lines. |
| Type recipient name and address and press C-M-c | Display Type recipient name and address and press C-M-c on the screen. |
| C-a | Move to the beginning of the line. |
| C-u C-x q | Enter a recursive edit. |
| C-M-c | Exit the recursive edit. |
| C-e | Move to the end of the line. |
| M-5 Enter | Move the cursor down 5 lines. |
| Type date and press C-M-c | Display Type date and press C-M-c on the screen. |
| C-a | Move to the beginning of the line. |
| C-u C-x q | Enter a recursive edit. |
| C-M-c | Exit the recursive edit. |
| C-e | Move to the end of the line. |
| M-5 Enter | Move the cursor down 5 lines. |
| Dear Space | Display Dear on the screen. |
| F4 or C-x ) | End keyboard macro definition. |