new article on emacs keyboard macro as well as elisp code.
• 〈Emacs Keyboard Macro and Examples〉
http://xahlee.org/emacs/emacs_macro_example.html
plain text version follows
--------------------------------------------------
Emacs Keyboard Macro and Examples
Xah Lee, 2010-11-16
This page shows a example of real world use of emacs keyboard macro
system, and a emacs lisp example.
Emacs has a feature that lets you record a sequence of keystrokes. I
learned the basics since i started to use emacs in 1998, but actually
never used it much, maybe few times a year. Since 2007, i learned the
command “apply-macro-to-region-lines”, which made it more useful.
Still, i use it perhaps once a month. However, when you use it, it
saves you hours for doing the job.
First, let's see what it does.
How to record a sequence of keystrokes?
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.
To run the keystrokes you've recorded, press 【Ctrl+x e】 (kmacro-end-
and-call-macro) or call “apply-macro-to-region-lines”.
Command Name Key
start-kbd-macro 【Ctrl+x (】
end-kbd-macro 【Ctrl+x )】
call-last-kbd-macro 【Ctrl+x e】
apply-macro-to-region-lines ◇
Saving Your Keyboard Macro for Future Use
If you want to use your keyboard macro (kmacro) for future use, you
can save it. To save the macro:
* (1) Call “name-last-kbd-macro”.
* (2) call “insert-kbd-macro”. This will insert the lisp code for
the kmacro at the cursor point.
* (3) Put the code in your emacs init file.
* (4) Then, you can execute your kmacro like this: 【Alt+x
‹yourMacroName›】.
Turn Off line-move-visual
When you record keystrokes, it is better that the arrow up/down keys
move the cursor thru 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.
Example of Actual Use
Here's a example of how i use keyboard macro. Usually, i use macro
about once or few times a month. When you need it, it is extremely
time saving. Otherwise you might spend hours doing it, or even if you
know elisp well.
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.
I have 60 other lines like this:
http://xahlee.org/PageTwo_dir/Personal_dir/dvorakKeymap.txt
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
...
I've written a elisp code that turns a url on my site into the format
i want. Here's the lisp code:
(defun xah-cite ()
"Change the file path under cursor into title and path.
For example, this line
/Users/xah/web/xahlee_org/emacs/emacs.html
becomes
• 〈Xah's Emacs Tutorial〉
http://xahlee.org/emacs/emacs.html
The title came from HTML file's title tag.
File path must be a url scheme, full path, or relative path.
Example url schemes among browsers and OSes
C:\\Users\\xah\\web\\xahlee_org\\emacs\\emacs.html ; ie
file:///C:/Users/xah/web/xahlee_org/emacs/emacs.html ; firefox
file:///C:/Users/xah/web/xahlee_org/emacs/emacs.html ; google chrom
file:///C:/Users/xah/web/xahlee_org/emacs/emacs.html ; safari
file://localhost/C:/Users/xah/web/xahlee_org/emacs/emacs.html ; opera
file:///Users/xah/web/xahlee_org/emacs/emacs.html ; safari
file:///Users/xah/web/xahlee_org/emacs/emacs.html ; firefox
file://localhost/Users/xah/web/xahlee_org/emacs/emacs.html ; opera
Example full path variations
c:/Users/xah/web/xahlee_org/emacs/emacs.html
/Users/xah/web/xahlee_org/emacs/emacs.html
This is Xah Lee's personal command assuming a particular dir
structure."
(interactive)
(let (bds ff title)
(setq bds (bounds-of-thing-at-point 'filename))
(setq ff
(buffer-substring-no-properties
(car bds)
(cdr bds)))
;; change file path to full path
(setq ff (replace-regexp-in-string "^http://www\\." "http://" ff))
(setq ff (replace-regexp-in-string "/$" "/index.html" ff))
(setq ff
(cond
((string-equal system-type "windows-nt") ; Windows
(replace-regexp-in-string "^http://xahlee\\.org/" "c:/
Users/xah/web/xahlee_org/" ff)
)
((string-equal system-type "darwin") ; Mac
(replace-regexp-in-string "^http://xahlee\\.org/" "/Users/
xah/web/xahlee_org/" ff)
)
)
)
(setq ff (local-url-to-file-path ff))
(setq ff (expand-file-name ff ))
(setq title
(if (string-match ".+html\\'" ff)
(get-html-file-title ff)
(file-name-nondirectory ff)))
(delete-region (car bds)
(cdr bds))
(insert "• 〈" title "〉\n" (xah-web-path-to-url ff))
))
I have defined a alias for this command to save me typing, because i
use it often. Alias is defined like this:
(defalias 'c 'xah-cite)
So, to call my command, i type 【Alt+x c Enter】 (Actually i type “Alt
+a” instead of “Alt+x”, because i'm using ErgoEmacs Keybinding.). This
can easily change each line to the format i want. But there are 60
lines! So, a keyboard macro comes to use. Here's how i do it.
* Move cursor to the beginning of first url.
* Type 【Ctrl+x (】 to start recording.
* Type 【Alt+x c Enter】. To change the line to the way i want.
* Type Enter to insert a blank line.
* Type → to move to the beginning of next url.
* Type 【Ctrl+x )】 to end macro recording.
Then, i select all lines that has not been processed, then call “apply-
macro-to-region-lines”. Then all lines becomes this:
• 〈dvorakKeymap.txt〉
http://xahlee.org/PageTwo_dir/Personal_dir/dvorakKeymap.txt
• 〈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
...
Emacs is fantastic!
In keyboard macro, you can call any emacs command, or even more than
one command. Emacs's kmacro feature simply play back the keystrokes.
In general, kmacro is extremely useful whenever you need to do
something repeatitive and the steps are the same. It is a great time-
saver. You do not need to know elisp to use it. Even if you are a
elisp master, many repeatives jobs 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 30 min.
Xah