This page shows you how to use emacs keyboard macro feature, with several examples of real world use.
The keyboard macro (kmacro) feature that lets you record and playback keystrokes. The key strokes can include calling emacs commands or commands that execute any emacs lisp functions.
To record keystrokes, press 【Ctrl+x (】 (kmacro-start-macro), then start typing your keystrokes. When done, press 【Ctrl+x )】 (kmacro-end-macro). This records your keystrokes.
If you made a mistake, you can cancel 【Ctrl+g】 (keyboard-quit) and start over.
To run the keystrokes you've just recorded, call call-last-kbd-macro. Here are the common kmacro commands:
| Command Name | Key |
|---|---|
start-kbd-macro | 【Ctrl+x (】 |
end-kbd-macro | 【Ctrl+x )】 |
kmacro-end-and-call-macro | 【Ctrl+x e】 |
call-last-kbd-macro | ◇ |
apply-macro-to-region-lines | ◇ |
Note that kmacro-end-and-call-macro is just like call-last-kbd-macro, except that if a kmacro recording is still on-going, it ends it first.
If you want to use your keyboard macro for future use, you can save it. To save the macro:
name-last-kbd-macro and give it a name.insert-kbd-macro. This will insert the lisp code for a named kmacro at the cursor position.Once you've saved your macro with a name, you can also give it a keyboard shortcut, such as F8.
When you record keystrokes, it is better that the arrow up/down keys move the cursor by a logical line, as opposed to visual line. (Emacs 23's default is visual line.) To set to logical line, call set-variable, then give line-move-visual, with value “nil”. (“t” for true; “nil” for false).
When you play back macro, be sure the line-move-visual is the same as when you recorded it.
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://xahlee.org/3d/3D_inputs.html
I need it to be like this:
• 〈Mathematical Models of 3D Inputs Control〉 http://xahlee.org/3d/3D_inputs.html
Where the first line is the title of that page.
Here are some of my lines:
http://xahlee.org/Periodic_dosage_dir/Logitech_G13_Gameboard.html http://xahlee.org/Periodic_dosage_dir/bangu/pinyin_frequency.html http://xahlee.org/Periodic_dosage_dir/logitech_trackball.html http://xahlee.org/Periodic_dosage_dir/mouses.html http://xahlee.org/Periodic_dosage_dir/trackball.html …
Here's what i do to record this as keyboard macro.
http://xahlee.org/ by a web root ~/web/xahlee_org/ 〔☛ Emacs: Find/Replace Tutorial〕ffap (find-file-at-point) to open the file.<title>…</title> tag, by using isearch-forward for <title>, set-mark, then search for <, move cursor 1 position back, then copy it.[], move cursor in, paste the title. (you can later use query-replace to replace the […] by • 〈…〉. (or you can insert the bullet and angle brackets directly. 〔☛ Emacs & Unicode Tips〕)Now, each time you call 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 call apply-macro-to-region-lines. Then, in about 1 second, all 60 lines become this:
• 〈Logitech G13 Advanced Gameboard Review〉 http://xahlee.org/Periodic_dosage_dir/Logitech_G13_Gameboard.html • 〈Chinese Pinyin Letter Frequency and Dvorak Layout〉 http://xahlee.org/Periodic_dosage_dir/bangu/pinyin_frequency.html • 〈Logitech Trackball Mouse Reviews〉 http://xahlee.org/Periodic_dosage_dir/logitech_trackball.html • 〈Review Of Microsoft SideWinder Gaming Mouses〉 http://xahlee.org/Periodic_dosage_dir/mouses.html • 〈Trackball Mouse Reviews〉 http://xahlee.org/Periodic_dosage_dir/trackball.html …
In keyboard macro, you can call any emacs command, or even more than one command. Emacs's kmacro feature 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.
Here's another example of kmacro use.
I have a webfeed file 〔blog.xml〕 in Atom Webfeed format. There are many entries like this:
<entry> <title>…</title> <id>…</id> <updated>…</updated> <summary>…</summary> <content type="xhtml"> <div xmlns="http://www.w3.org/1999/xhtml"> <p>see <a href="http://xahlee.blogspot.com/2011/11/emacs-lisp-exercise-insert-random-uuid.html">http://xahlee.blogspot.com/2011/11/emacs-lisp-exercise-insert-random-uuid.html</a></p> </div> </content> <link rel="alternate" href="http://xahlee.org/emacs/blog.html"/> </entry>
I need to remove the “content” part and simply put the URL in the alternate link, like this:
<entry> <title>…</title> <id>…</id> <updated>…</updated> <summary>…</summary> <link rel="alternate" href="http://xahlee.blogspot.com/2011/11/emacs-lisp-exercise-insert-random-uuid.html"/> </entry>
Kmacro is excellent solution for this. You just call interactive search to move cursor to places you want, and do delete or copy & paste. Like this.
isearch-forward 【Ctrl+s】 for http://xahlee.blogspot.isearch-backward 【Ctrl+r】 for ", move cursor forward 1 position, set mark.isearch-forward for ", do a copy. Now the blog URL is in clipboardisearch-backward for <content , set mark.isearch-forward for </content>.isearch-forward for href=.Once i recorded these steps, then i call kmacro-end-and-call-macro 【Ctrl+x e】. This way, every 2 keystrokes makes one entry change. This allows me to visually verify what i've done is correct. I could also press 【Ctrl+u】, then a number, then 【Ctrl+x e】 to repeat it automatically n times.
Here's another example of kmacro use.
I have a function insert-random-uuid 〔☛ Emacs Lisp Exercise: insert-random-uuid〕 I want to call it hundreds of times to see its output. Let's just say i want to call the elisp expression (random 100) one hundred times.
One way is to write a elisp command on the spot, like this:
(defun xx-random-test () "test" (interactive) (dotimes (ii 100) (insert (format "%d " (random 100)))) )
But that takes 5 minutes to write, plus you need to have elisp knowledge. You can use a kmacro to do this fast, with the following steps.
eval-expression, with this expression (random 100). The 【Ctrl+u】 will make eval-expression insert its result in current buffer.kmacro-end-and-call-macro 【Ctrl+x e】.For more example of kmacro use, see: