Sams Teach Yourself Emacs in 24 Hours |  | ContentsIndex
Hour 13: Macros |  | Previous HourNext Hour
Sections in this Hour: |  |  |  |

## Hour 13
Macros

The most wonderful thing about Emacs is its capability to do the trivial part of your work for you. Sometimes having Emacs do your work takes more time than solving the original task, but you have still learned two things:

- You have learned a bit more about Emacs.

- Monotonous work often tends to be error prone because you inattentively do it over and over again. This is avoided by letting Emacs do it for you.

Emacs has two ways to do your work for you. The complex way is by programming the solution in Lisp; the other and easier way is to develop a macro. Programming Lisp is beyond the scope of this book, but fortunately macros can do the job in many situations. (You will, however, learn a little about Lisp in Hour 22, "Learning Lisp Basics.")

I guess it's time to tell you what a macro is, right? A macro is a recorded sequence of keystrokes that you can execute over and over again. If you doubt that this should do all your monotonous work for you, please think about all the actions that you do very often. The following are examples where a macro might help you typing:

- Making the first character of each paragraph uppercase. (You can press M-c (capitalize-word) to capitalize the word in front of point).

- Renumbering all sections of a document due to the fact that a section is inserted in the middle.

- Replacing every occurrences of printf(...) with write {...}. (No, you cannot use search-and-replace, because this would miss the replacement of the end parenthesis with the end brace.)

Tip - If you ever catch yourself in a situation in which you type the same word over and over again and the result is that the keyboard makes a weak sound (it sounds like tik taak tik tik ), it might be worth it to create a macro!

There are two kinds of macros:

- Those that do a task repeatedly over several lines--for example, capitalizing every paragraph.

- Those that do a complicated action for you (such as inserting some text and repositioning point to the middle of this text). These macros are not intended to be repeated several times after each other but rather to be executed once in awhile when you need the keystrokes of the macro executed.

In the first kind of macro, you often want to get the macro executed a certain number of times or within a specific region of text. This is described in detail in the section "Repeating Macros". In the second kind of macro, you often want to build a macro that interacts with you to insert some text, for example to fill out a form. This is described in the section "Recording a Macro for Filling Out a Simple Text-Based Database." You might sometimes want to save a macro for later Emacs sessions. This is described in the section "Saving a Macro for Later Sessions."

Caution - In the Emacs documentation, macros are referred to by the name keyboard macros . The reason for this is that macros in Lisp have nothing to do with the macros discussed in this hour.

## Writing a Simple Macro

Define a macro by telling Emacs to record the keystrokes to come, doing what your macro should do, and finally telling Emacs that you are finished defining the macro.

#### Recording and Executing Macros

To let Emacs type for you, you can record a sequence of keys that it can retype at your command. Follow these steps:

  1. Press C-x ( (start-kbd-macro) to tell Emacs to start recording your macro.

  2. Do whatever you want Emacs to do for you later. This can include typing text, moving point, changing buffers, and executing commands. Press C-x ) (end-kbd-macro) to tell Emacs to stop recording the macro.

  3. To execute the macro just recorded type C-x e (call-last-kbd-macro).

If some commands during the recording of the macro ring the bell (that is, an error occurs), the macro is automatically aborted. To manually abort the recording of a macro, simply press C-g (keyboard-quit).

#### Capitalize the First Character of Each Section

This task shows you how to capitalize the first character of each paragraph by using a macro. Follow these steps:

  1. Go to the beginning of the first section you want to capitalize. Press C-x ( (start-kbd-macro). This starts recording the macro. Note that the text Def is shown in your modeline to indicate that you are recording a macro.

  2. Press M-c (capitalize-word). This capitalizes the first word in the paragraph.

  3. Press M-} (forward-paragraph). This takes you to the beginning of the next paragraph.

  4. Press C-x ) (end-kbd-macro). This stops recording the macro. Now you can press C-x e (call-last-kbd-macro) several times to capitalize several paragraphs.

Caution - For a macro such as the preceding to work successfully, it needs to finish in a position that can be used as a starting point to be repeated again. In the preceding example, the starting point is the beginning of a paragraph. When the macro has been executed, it places point at the start of the next paragraph.

Note - Only one macro can be current at a time. To execute the current macro, press C-x e. To have several macros available at one time, you need to give each one a name. When they have names, they are available by pressing M-x. This is described in the section "Saving a Macro for Later Sessions."

Sams Teach Yourself Emacs in 24 Hours |  | ContentsIndex
Hour 13: Macros |  | Previous HourNext Hour
Sections in this Hour: |  |  |  |

© Copyright Macmillan USA. All rights reserved.