Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 13: Macros

Previous HourNext Hour

Sections in this Hour:

 

Further Interaction with the Macro


Sometimes you might want to alternate repeatedly between letting a macro do some work and writing something yourself. You could implement this by creating macros that do all the macro actions, calling them one at a time, and typing in between. But, of course, Emacs has a method that is much smarter.

In Hour 7, you saw how you could pause a search-and-replace action to edit an error you have found. This was called recursive-edit. The same mechanism is available during macro execution.

When you record your macro, press C-u C-x q (kbd-macro-query with a C-u prefix). When your macro is executed later, the macro enters recursive editing at this point. In other words, it lets you do some editing before it continues. To tell it to continue, you must press M-C-c (exit-recursive-edit).

There is one minor thing that is important to understand here, which is that the macro recording itself is paused when you press C-u C-x q. To continue recording the macro, press C-M-c.

Recording a Macro for Filling Out a Simple Text-Based Database

Follow these steps to learn how pausing macros can help you fill out an entry in a simple database with fields called Name, Street, and so on:

1. Press C-x ( (start-kbd-macro) to record the macro.

2. Type Name:, which should be the first entry in the database.

3. Press C-u C-x q (kbd-macro-query) to make the macro pause. What you type now will not be part of the macro, so you can fill out the first entry in your database. Type a name.

4. Press M-C-c (exit-recursive-edit) to continue recording the macro.

5. Type Street:, which is the second entry in your simple database.

6. Press C-u C-x q again to enter recursive edit.

7. Again, the following text is not part of the macro, so here you fill out the location of the person that you are inserting. Press M-C-c (exit-recursive-edit) to continue recording the macro.

Continue this way until you have all the fields for your database.

8. Press C-x ) (end-kbd-macro) to stop recording your macro.

Caution - Emacs does not enable you to exit the macro during recursive edit and, thus, abort recording the macro. You have to leave recursive edit before you can abort recording the macro.


In Figure 13.2, you can see an example of the database macro that you have defined.

Figure 13.2
Execution of the macro recorded.

Please note the square brackets around (Text) in the figure. This indicates that the macro is waiting for you to let it continue. (Technically, it does mean that you are editing recursively, but that is the same in this case.)

Tip - If you want to abort the macro when you are in recursive-edit, you can press C-] (abort-recursive-edit). This also applies if you find that you forgot to finish a macro and, therefore, are in recursive editing.

It might be easier to remember this keybinding if you notice that the key is the same as the indication in the mode line (that is, the end square bracket).


Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 13: Macros

Previous HourNext Hour

Sections in this Hour: