Emacs Key Macro Example: Add HTML Attribute

By Xah Lee. Date: . Last updated: .

This page shows a real world example of emacs keyboard macro use. If you are not familiar with keyboard macro, see: Emacs: Keyboard Macro.


I have a HTML page that are a list of 34 books in HTML format.

Each citation is like this:

<span class="title">Geometry: Euclid and Beyond</span>
<a class="amz" href="http://www.amazon.com/dp/0387986502/?tag=abc-20">amazon</a>

I need the amazon link to contain a “title” attribute. Like this:

<span class="title">Geometry: Euclid and Beyond</span>
<a class="amz" href="http://www.amazon.com/dp/0387986502/?tag=abc-20" title="Geometry: Euclid and Beyond">amazon</a>

To manually fix this:

  1. Alt+x isearch-forward to find a amazon link
  2. then search backward looking for the class="title" to find the title of the book.
  3. Move cursor into the markup to copy the title.
  4. Search forward to the amazon link again, type title=" then paste, then type ".

do this 34 times?


Emacs keyboard macro came to rescue me!

Basically, use isearch forward/backward to move cursor to right positions, mark and copy, then move to the right position to paste. Here are exact steps of the keyboard macro:

  1. Go to the top of the page. 【Ctrl+Home
  2. Type 【Ctrl+x (】 to start recording.
  3. Type 【Ctrl+s】 to start isearch.
  4. Type abc-20"> to find a amazon link.
  5. Type Enter ↵ to exit the isearch and leave the cursor there.
  6. Type 【Ctrl+r】 to start backward isearch.
  7. Type class="title"> to find the title markup.
  8. Type 【Ctrl+】 3 times to move the cursor into the text content of the title markup.
  9. Type 【Alt+Shift+8】 (select-text-in-quote) to select all the text between the > and < (select-text-in-quote is a command available in ErgoEmacs Keybinding package.)
  10. Type 【Ctrl+c】 key to copy the title text.
  11. Type 【Ctrl+s】 to start isearch then search for abc-20" again and place cursor there.
  12. Type title=".
  13. Type 【Ctrl+v】 to paste.
  14. Type ".
  15. Type 【Ctrl+x )】 to end macro recording.

Then, i move cursor to the top of page, then press 【Ctrl+x e】 to run my kmacro. Each time i do this, one link is fixed. About 30 keystrokes is reduced to just 2 key strokes. I can run my kmacro once for every link to fix, a total of 34 times, or, i can just press 【Ctrl+u 34】 then 【Ctrl+x e】, fixing all links in one shot.

This seems complicated, but actually is done within one minute. If i were to write a elisp, it'll take 30 minutes or so.

In this case, i actually call the macro one at a time, instead repeating it 34 times in one-shot, because, often you do not know that the page necessarily have a uniform format. So, doing it one at a time lets me eyeball the change.

This is the beauty of emacs, where everything is interactive. When you work with computer language source code, or sys admin scripts, the texts are almost never in some uniform format. With emacs, you edit the text as usual, but as soon as you see a pattern, you can start a keyboard macro that lets you automate all mechanical steps. When you find the format changes, you can manually fix some irregularity, or just start a new keyboard macro when you see a new pattern.

See also Why Emacs is Still so Useful Today and Emacs Lisp Power: Text-Soup Automation.

Liket it? Put $1 at patreon. Or Buy Xah Emacs Tutorial. Thanks.