Emacs: Key Macro Example: Add Title of URL
Here's a example of keyboard macro use. I use macro about few times a month. When you need it, it is extremely convenient.
Today, i have about 60 lines, each line is a URL, like this:
http://example.com/3d/3D_inputs.html
I need it to be like this:
* Mathematical Models of 3D Inputs Control http://example.com/3d/3D_inputs.html
Where the first line is the <title>…</title>
of that page.
Here's what i do to record this as keyboard macro.
- First, replace all
http://example.com/
by local path~/web/example_com/
〔see Emacs: Find Replace in Current File〕 - Move cursor to the first line.
- Start kmacro.
- Alt+x
find-file-at-point
to open the file. - Ctrl+Home (Move cursor to top.)
- Grab the HTML title between
<title>…</title>
tag, by usingisearch-forward
for<title>
, set-mark, then search for<
, move cursor 1 position back, then copy it. - Close the file by Alt+x
kill-buffer
. - Type
*
, then paste (yank
) the title, then Enter to insert a line. - Move cursor 2 position down to the next line.
- End the kmacro.
Now, each time you Alt+x call-last-kbd-macro
, a line will be processed, with its title inserted above the line.
You can also select all lines that has not yet been processed, then Alt+x apply-macro-to-region-lines
. Then, in about 1 second, all 60 lines become this:
* Logitech G13 Advanced Gameboard Review http://example.com/kbd/Logitech_G13_Gameboard.html * Pinyin Letter Frequency on Dvorak Layout. 拼音字母頻率, Dvorak 鍵盤佈局 http://example.com/musing/pinyin_frequency.html * Trackball Reviews http://example.com/kbd/trackball_index.html …
In keyboard macro, you can call any emacs commands. Emacs's kmacro simply play back the keystrokes.
keymacro is extremely useful whenever you need to do something repeatitive. It is a great time-saver. You do not need to know elisp to use it. Even if you are a elisp expert, many repeative tasks are still best done with a kmacro. With kmacro, i can finish this job in 30 seconds. But if i were to write a elisp for it, it easly can take 20 min and drains a lot brain juice.