Friday, August 12, 2005

Emacs Keyboard Macros

Keyboard macros are one the most useful tools in Emacs. I will start with a simple example.

Let's assume you have a website, and on one of the pages you have lists of fruit. The typing of these lists is becoming a lot of work, but you don't have database and programming skills.

Let's see how to create these lists with emacs. First type the list of items you would like in the lists. This is still needed because, it is the changing part in the lists.

apple
banana
pear

Something like that. Now to create the list items li, you have to type only one of those.

  1. Move point (the cursor) to the first item in the list, apple.
  2. Type C-x ( (start macro). All the keystrokes after this will be recorded.
  3. Type the begin tag, <li>.
  4. Go to the end of the line with C-e.
  5. Type </li> here.
  6. To make the macro even more useful, move to beginning of the next line with C-n C-a.
  7. Now type C-x ) (stop macro) to stop the recording of the macro.

With C-x e you can now execute the macro again. Do this until you are at the end of the list. That was all. Now you have a list that looks like this.

<li>apple</li>
<li>banana</li>
<li>pear</li>

As you may have noticed, the pattern is create a list of the text that is different on every row. Then create a macro for one line and move to the next line. Apply the macro on all the lines.

Of course there is still a lot more to learn about emacs keyboard macros, but I will save that for another time.

0 Comments:

Post a Comment

<< Home