Emacs Macros and the Power-Macros Package LG #47

Rate this post


People sometimes tend to forget that computers are a tool that can make their life much easier. One of the things computers are especially good at, and which is easy for you to teach, is monotonic repetitive work. It gets even better: This kind of work also seems to be the work humans are worst at doing that is, monotonic repetitive work tends to be very error-prone.

Emacs can eliminate the repetitive work with a very useful concept called macros. Macros are basically keystrokes that Emacs types for you.

This article will teach you about Emacs macros and show you a number of useful examples. Furthermore, it will teach you about an Emacs package I written called power-macros, which makes it very easy to bind macros to keys and to save them to a file for use in later Emacs sessions.

Defining an Emacs macro.

Defining an Emacs macro is done by pressing C-x ( (That is press Ctrl, hold it down and press x, and then release Ctrl and x and press the opening bracket). The subsequent keystrokes will be part of your macro that is whenever you ask Emacs to execute your macro, these keystrokes will be typed for you. When you are done defining the macro, press C-x )

When a macro has been defined you may ask Emacs to imitate your keystrokes as often as you want simply by pressing C-x e.

Two-cent tip

If you need to repeat macros several times, then it might be quite annoying that you need to press two keys to execute the macro defined. (That is, if you need to execute the macro three times, then you must press C-x e, C-x e, C-x e). A solution to this may be to bind execute last defined keyboard macro to a single key press. This way, you may for example bind it to shift-F1, by inserting the following code into your .emacs file:

(global-set-key [(shift f1)] 'call-last-kbd-macro)

Example: Making the current word bold

That’s it now you have learned the basics about Emacs macros, but I’m pretty sure that you haven’t had the feeling yet that this would change your world much, right? To be honest, I’ve used Emacs for more than seven years, but until less than a year ago, I didn’t see the light either… Therefore, here comes a small example to vet your appetite. More will follow later in the article.

Imagine that you often want to make the current word in boldface (in HTML documents), you could simply do that by inserting and around the word. That’s no big job, but if you are copy-editing a book, where you need to make words in boldface hundreds of times each hour, then a macro, that can do this may really spare you a lot of time.

The macro is easily recorded: Go to the beginning of the word, insert , go to the end of the word, insert , and there you are!

Ohhh, not so fast! There is one very important point to notice about this you are not allowed to go to the beginning respectively the end of the word by pressing the arrow key a number of times! Why not? Well if you do, then the macro will fail to find the border of the word if your word is of a different length than the word used when defining the macro. You must therefore instead use the commands forward-word and backward-word. These commands are bound to control and the arrow keys. Thus to go to the beginning of a word, simply press control and the left arrow key.

Lire aussi...  News Bytes LG #64

Basically there exist two kinds of macros: those that are used now and again, and those that are used a number of times in a row and then never used again. The above is an example of a macro of the first kind. The description of the second kind is out of the scope for this article, but an example could be a macro, that added /* REMOVE: to the beginning of a line, and */ to the end of a line. You may use such a macro a number of times in a row, to comment out a whole function in C for later removal.

Making macros more general

In some C++ programs you will often find constructs which resemble the following:

for (bool cont=iterator.First(value); cont; cont=iterator.Next(value)) {

The only difference from occasion to occasion is the names cont, iterator, value, and of course the content in between the curly brackets.

If you insert the code above often, then you may wish to build a macro, which will help you with this. Your first attempt may be to define a macro, which simply inserts:

for (bool =.First(); ; =.Next()) {

That is, a macro that simply leaves out all the parts that may change from time to time. This is, however, not as useful as it could be, simply because you would need to type cont three times, and iterator and value each two times. What you really would need was for Emacs to ask you about the names to use instead of cont,iterator, and value.

Guess what? you can do that with macros! The trick is called recursive editing. With recursive editing you tell Emacs to stop at a specific place in the macro, you do some editing, and when you are done you tell Emacs to continue with the macro.

When you record the macro, you may tell Emacs to enter recursive editing by pressing C-u C-x q. Then whenever you execute the macro Emacs will stop macro execution at that point and let you do some editing, and the macro will first continue when you press C-M-c (That is control-meta-c, if there is no meta key on your keyboard it is most likely the alt key instead.).

While you record the macro, Emacs will also enter recursive editing at that point. That is, the editing you do from the point you press C-u C-x q and till you press C-M-c will not be part of the macro.

Ok, we are almost ready to develop a very neat and useful macro, but first lets exercise what we’ve learned above with a simple example. Type the following:

C-x ( Type a word ==> C-u C-x q

Now type Hello World, and when done, continue typing the following:

C-M-c Hello World, and when you press C-M-c, it will continue with the macro, which means that it will insert the text