You need to remember three keyboard shortcuts to use keyboard macros in emacs.
C-x (
— start-kbd-macroC-x )
— stop-kbd-macroC-x e
— call-last-kbd-macro
Type, C-x (
hello
C-x )
C-x e
C-x e
. You will have
“hellohellohello”.
Let’s do something more useful like converting a list of words in to a HTML list of links. That is, given two words
contact about
convert them into
<li><a href="/contact.html">contact</a></li>
<li><a href="/about.html">about</a></li>
Position the cursor in the first column of ‘contact’ and
C-x ( -- start recording M-x just-one-space -- remove preceding white-space C-SPC -- set mark C-e -- go to end of line M-w -- yank selected text C-a -- go the beginning of the line and type <li><a href="/ C-y -- paste the text and type .html"></a></li> C-a -- go back to column 0 C-n -- go to next line C-x ) -- stop recording
To play back the record on subsequent lines, press C-x e
as many number of times you want. To execute the macro n
times,
say 5, press C-u 5 C-x e
.