Emacs Keyboard Macro Example: Add Title of URL

By Xah Lee. Date: . Last updated: .

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.

  1. First, replace all http://example.com/ by local path ~/web/example_com/ [see Emacs: Find and Replace Commands]
  2. Move cursor to the first line.
  3. Start kmacro.
  4. Alt+xffap (find-file-at-point) to open the file.
  5. Move cursor to top. 【Ctrl+Home
  6. Grab the HTML title between <title>…</title> tag, by using isearch-forward for <title>, set-mark, then search for <, move cursor 1 position back, then copy it.
  7. Close the file by Alt+x kill-buffer.
  8. Type * , then paste (yank) the title, then Enter ↵ to insert a line.
  9. Move cursor 2 position down to the next line.
  10. 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.

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