: Learning GNU Emacs, 3rd Edition

6.10 Beyond Macros

6.10 Beyond Macros

Macros are an important tool for streamlining repetitive editing. They let you write your own commands for performing complex tasks without needing to know anything more than you already know: the basic Emacs commands for moving around and manipulating text. Even if you're an Emacs novice, you should be able to use macros with little difficulty.

However, Emacs is almost infinitely flexible, and macros cannot do everything. In many situations, there's no substitute for writing a Lisp function that does exactly what you want. If you know Lisp or would like to learn some, you can write your own Lisp functions to do more complex tasks than keyboard macros can handle. Chapter 11 covers the basics of writing Lisp functions.

Table 6-6 summarizes macro commands.

Table6-6.Macro commands

Keystrokes Command name Action
C-x ( kmacro-start-macro Start macro definition.
F3 kmacro-start-macro-or-insert-counter Start macro definition. If pressed while defining a macro, insert a counter.
C-x ) kmacro-end-macro End macro definition.
F4 kmacro-end-or-call-macro End macro definition (if definition is in progress) or invoke last keyboard macro.
C-x e kmacro-end-and-call-macro Execute last keyboard macro defined. Can type e to repeat macro.
C-x C-k n name-last-kbd-macro Name the last macro you created (before saving it).
(none) insert-kbd-macro Insert the macro you named into a file.
(none) macroname Execute a named keyboard macro.
C-x q kbd-macro-query Insert a query in a macro definition.
C-u C-x q (none) Insert a recursive edit in a macro definition.
C-M-c exit-recursive-edit Exit a recursive edit.
C-x C-k b kmacro-bind-to-key Bind a macro to a key (C-x C-k 0-9 and A-Z are reserved for macro bindings). Lasts for current session only.
C-x C-k Space kmacro-step-edit-macro Edit a macro while stepping through it (in our opinion, the interface is overly complex).
C-x C-k l kmacro-edit-lossage Turn the last 100 keystrokes into a keyboard macro. If any mouse clicks are among the last 100 keystrokes, does not work.
C-x C-k e edit-kbd-macro Edit a keyboard macro by typing C-x e for the last keyboard macro defined, M-x for a named macro, C-h l for lossage, or keystrokes for a macro bound to a key.
C-x C-k Enter kmacro-edit-macro Edit the last keyboard macro.
C-x C-k C-e kmacro-edit-macro-repeat Edit the last keyboard macro again.
C-x C-k C-t kmacro-swap-ring Transpose last keyboard macro with previous keyboard macro.
C-x C-k C-d kmacro-delete-ring-head Delete last keyboard macro from the macro ring.
C-x C-k C-p kmacro-cycle-ring-previous Move to the previous macro in the macro ring.
C-x C-k C-n kmacro-cycle-ring-next Move to the next macro in the macro ring.
C-x C-k r apply-macro-to-region-lines Apply this macro to each line in a region.

: 2.304. /Cache: 3 / 0