Keyboard macros

From WikEmacs
Revision as of 14:49, 25 March 2012 by 101.63.150.116 (Talk)

Jump to: navigation, search

Basic Keyboard Macro (`(info "(emacs) Basic Keyboard Macro")')

Keyboard macros can be used to automate or repeat tedious editing tasks in Emacs.

Example usage

Consider the standard *scratch* buffer:

;; This buffer is for notes you don't want to save, and for Lisp evaluation.
;; If you want to create a file, visit that file with C-x C-f,
;; then enter the text in that file's own buffer.

Suppose you want to remove the first occurrence of the letter "a" in that piece of text. You could write a regular expression to do the job, but let's assume you want to use a keyboard macro this time.

  1. Make sure point is at the start of the buffer.
  2. Hit C-x ( to start recording your macro. Note: If you hit C-g or if an error occurs, your keyboard macro recording will stop.
  3. Hit C-s followed by a to find the first "a". Now, point is right after the first "a" in the text.
  4. Hit backspace to delete that "a".

The first occurrence of "a" of the first line has been deleted. Let's move point to the beginning of the next line and then stop recording.

  1. Hit C-e C-f to move point to the beginning of the next line.
  2. Hit C-x ) to finish the recording of our macro.

The macro you have just recorded performs the operation of removing the first occurrence of "a" it can find and then moving point to the next line.

  1. Hit C-x e once to call that macro.
  2. Continue hitting e to call it several times. Hit any other key to get out of the macro repetition.

Repeating a macro

To repeat a macro e.g. five times, you could hit C-x e e e e e. This could become tedious if you want to repeat it a greater amount of times, so an alternative is to provide a numeric argument to C-x e, e.g. C-u 25 C-x e.

Repeating until there's an error

Provide a 0 prefix argument to C-x e to execute a macro from point until there is an error, e.g. C-u 0 C-x e or M-0 C-x e. See prefix arguments.

Repeating a macro on every line in a region

If you've recorded a macro which you want to call on every line in a buffer, you can use C-x C-k r, which applies the macro to every line in the region.

Saving macros

keyboard macros/(emacs) Save Keyboard Macro.html#(emacs) Save Keyboard Macro {{{3}}} (`(info "(saving keyboard macros) {{{3}}}")')

By name

To give a macro a name which you can later call using M-x, hit C-x C-k n. Try to avoid strange characters in the name. Once you've given it a name, you can execute it using M-x the-name-of-the-macro.

Binding to a key

To bind a keyboard macro to a key using C-x C-k b. To avoid problems caused by overriding existing bindings, the key sequences C-x C-k 0 through C-x C-k 9 and C-x C-k A through C-x C-k Z are reserved for your own keyboard macro bindings. You can, however, bind a keyboard macro to whatever you like.

Saving it permanently

To save a keyboard macro for future Emacs sessions, go to your init file (or wherever you want to store it) and hit M-x insert-kbd-macro. Then enter the name of the macro you would like to save. This will insert Emacs Lisp code which defines the macro you chose, so you can use it in future sessions.

Using the example in the beginning of this article, let's say you named that macro remove-first-a. If you use insert-kbd-macro to insert the Lisp for that macro, this is what would come up:

(fset 'remove-first-a
   (lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item (quote ([19 97 13 backspace 5 6] 0 "%d")) arg)))

If you have bound the macro to a key as described in the previous section, you can generate the code for that as well by prefixing the insert-kbd-macro call with C-u.