Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 13: Macros

Previous HourNext Hour

Sections in this Hour:

 

Repeating Macros

Some of the macros are developed to be executed several times after each other, such as the one in the previous section. Sometimes you know exactly how many times you want the macro to be executed, and other times you have a region in which you want your macro to execute. Unfortunately, there are some times where you know neither of these things. You might, for example, know that you have exactly ten subsequent paragraphs, and you want these ten paragraphs to be capitalized.

An example of a macro which could be executed several times within a region is given in the previous section, where you developed a macro to capitalize paragraphs. Using this macro, you can capitalize every paragraph in a region. In other words, you can execute the macro until it finishes one execution with the point outside the region.

Tip - If you want to execute a macro several times in succession but you cannot determine a region or a number of times, you have to press C-x e (call-last-kbd-macro) several times. This might be irritating, because this is two keystrokes and difficult to do fast. A solution to this problem would be to bind the command call-last-kbd-macro to one of the function keys or a modified function key (for example, S-F1).


To execute a macro a number of times, you must give it a numeric prefix. Do this by pressing the Control key down, and, while holding it, typing the number. Thus to execute the previous defined macro 10 times, press and hold the Control key, type 10, release the Control key, and press C-x e. To be completely accurate, this is, of course, equivalent to pressing C-1 C-0 C-x e.

Repeating a Macro over a Region

The following are two approaches to repeating a macro over a region:

The first approach has the advantage that you do not need to worry about getting point to the correct location for the next iteration, but it has the drawback that you cannot create a macro, such as the one preceding, that capitalizes paragraphs throughout the region.

The first approach is available through the function apply-macro-to-region-lines, which comes with Emacs as standard. The other is available through the function sams-apply-macro-on-region, which is located on the CD as part of sams-lib.el. See Appendix A, "Installing Functions and Packages from the CD" for a description on how to install this library.

In the rest of this section, the second approach is described, because it is the most powerful one.

There are a few rules that must be followed when you record a macro that should be executed repeatedly with the command sams-apply-macro-on-region (some of them also apply when you record a macro for execution several times using a numeric prefix):

The macro which you defined in the previous section does, in fact, fulfill the preceding requirement. To uppercase paragraphs in a region, place the region in such a way that it starts at the first character of the first paragraph (this is important, because the macro starts by uppercasing the word at point) and press M-x (sams-apply-macro-on-region).

After you get used to this command, you will use it over and over again, so it's good advice to bind this command to a key.

Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 13: Macros

Previous HourNext Hour

Sections in this Hour: