Emacs Key Macro Example: Add HTML Attribute
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:
isearch-forwardto find a amazon link
- then search backward looking for the
class="title"to find the title of the book.
- Move cursor into the markup to copy the title.
- 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:
- Go to the top of the page. 【Ctrl+Home】
- Type 【Ctrl+x (】 to start recording.
- Type 【Ctrl+s】 to start isearch.
abc-20">to find a amazon link.
- Type Enter to exit the isearch and leave the cursor there.
- Type 【Ctrl+r】 to start backward isearch.
class="title">to find the title markup.
- Type 【Ctrl+→】 3 times to move the cursor into the text content of the title markup.
- Type 【Alt+Shift+8】 (select-text-in-quote) to select all the text between the
<(select-text-in-quote is a command available in ErgoEmacs Keybinding package.)
- Type 【Ctrl+c】 key to copy the title text.
- Type 【Ctrl+s】 to start isearch then search for
abc-20"again and place cursor there.
- Type 【Ctrl+v】 to paste.
- 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.
Emacs Keyboard Macro Topic
Patreon me $5. Ask me question on patreon