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.

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 memos, 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 that 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.) Nothing you type during the recursive edit becomes a part of the macro. You can type whatever you want to and then press ESC C-c to exit the recursive edit. Notice how the square brackets disappear when you type ESC C-c. When the square brackets are no longer on the screen, you have left the recursive edit. Anything you type at this point will become part of the macro. You can put as many pauses in your macros as you want to.

Example

Here’s an example of a macro that puts a memo template on the screen ...

Get Learning GNU Emacs, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.