Monday, April 21, 2008

Emacs Tip #18: Keyboard Macros

If you find yourself doing a repetitive set of keystrokes, but don't want to bother with learning how to actually write a script for that, then record a keyboard macro.


C-x ( begins recording
C-x ) ends recording
C-x e executes the last keyboard macro
C-x C-k lets you edit the keyboard macro you just wrote

If find yourself using lots of macros, you can even save them to your
.emacs and name them (for later use).

For more documentation, see the info page:

C-h K C-x (

Or check out the Keyboard Macros section in the Emacs wiki.

2 comments:

Mitch Wand said...

Keyboard macros are great, but the default keybindings are terrible.

Here's a better set, which I got from somebody (sorry, I've forgotten whom):

(global-set-key [f10] 'start-kbd-macro)
(global-set-key [f11] 'end-kbd-macro)
(global-set-key [f12] 'call-last-kbd-macro)

You can think of F10 as "start recording", F11 as "stop recording", and F12 as "play".

Jeremiah said...

Also handy here is M-x kmacro-bind-to-key, which will bind the last macro entered to a keystroke.