(ibuffer-default-display-maybe-show-predicates): New customizable
[bpt/emacs.git] / man / kmacro.texi
CommitLineData
eef59362 1@c This is part of the Emacs manual.
2935e788 2@c Copyright (C) 1985,86,87,93,94,95,97,2000,2001,2002,2003,2004
eef59362
KS
3@c Free Software Foundation, Inc.
4@c See file emacs.texi for copying conditions.
5@node Keyboard Macros, Files, Fixit, Top
6@chapter Keyboard Macros
eef59362
KS
7@cindex defining keyboard macros
8@cindex keyboard macro
9
10 In this chapter we describe how a sequence of editing commands can
11be recorded and repeated multiple times.
12
13 A @dfn{keyboard macro} is a command defined by the user to stand for
14another sequence of keys. For example, if you discover that you are
15about to type @kbd{C-n C-d} forty times, you can speed your work by
16defining a keyboard macro to do @kbd{C-n C-d} and calling it with a
17repeat count of forty.
18
19 You define a keyboard macro while executing the commands which are the
20definition. Put differently, as you define a keyboard macro, the
21definition is being executed for the first time. This way, you can see
22what the effects of your commands are, so that you don't have to figure
23them out in your head. When you are finished, the keyboard macro is
24defined and also has been, in effect, executed once. You can then do the
25whole thing over again by invoking the macro.
26
27 Keyboard macros differ from ordinary Emacs commands in that they are
28written in the Emacs command language rather than in Lisp. This makes it
29easier for the novice to write them, and makes them more convenient as
30temporary hacks. However, the Emacs command language is not powerful
31enough as a programming language to be useful for writing anything
32intelligent or general. For such things, Lisp must be used.
33
34@menu
35* Basic Keyboard Macro:: Defining and running keyboard macros.
36* Keyboard Macro Ring:: Where previous keyboard macros are saved.
37* Keyboard Macro Counter:: Inserting incrementing numbers in macros.
38* Keyboard Macro Query:: Making keyboard macros do different things each time.
39* Save Keyboard Macro:: Giving keyboard macros names; saving them in files.
40* Edit Keyboard Macro:: Editing keyboard macros.
41* Keyboard Macro Step-Edit:: Interactively executing and editing a keyboard macro.
42@end menu
43
44@node Basic Keyboard Macro
45@section Basic Use
46
47@table @kbd
48@item C-x (
49Start defining a keyboard macro (@code{kmacro-start-macro}).
50@item C-x )
51End the definition of a keyboard macro (@code{kmacro-end-macro}).
52@item C-x e
53Execute the most recent keyboard macro (@code{kmacro-end-and-call-macro}).
54First end the definition of the keyboard macro, if currently defining it.
55To immediately execute the keyboard macro again, just repeat the @kbd{e}.
56@item C-u C-x (
57Re-execute last keyboard macro, then add more keys to its definition.
58@item C-u C-u C-x (
59Add more keys to the last keyboard macro without re-executing it.
60@item C-x q
61When this point is reached during macro execution, ask for confirmation
62(@code{kbd-macro-query}).
63@item C-x C-k n
64Give a command name (for the duration of the session) to the most
8e887df4 65recently defined keyboard macro (@code{kmacro-name-last-macro}).
eef59362
KS
66@item C-x C-k b
67Bind the most recently defined keyboard macro to a key sequence (for
68the duration of the session) (@code{kmacro-bind-to-key}).
69@item M-x insert-kbd-macro
70Insert in the buffer a keyboard macro's definition, as Lisp code.
71@item C-x C-k e
72Edit a previously defined keyboard macro (@code{edit-kbd-macro}).
73@item C-x C-k r
8871f2ba 74Run the last keyboard macro on each line that begins in the region
eef59362
KS
75(@code{apply-macro-to-region-lines}).
76@end table
77
78@kindex C-x (
79@kindex C-x )
80@kindex C-x e
81@findex kmacro-start-macro
82@findex kmacro-end-macro
83@findex kmacro-end-and-call-macro
84 To start defining a keyboard macro, type the @kbd{C-x (} command
85(@code{kmacro-start-macro}). From then on, your keys continue to be
86executed, but also become part of the definition of the macro. @samp{Def}
87appears in the mode line to remind you of what is going on. When you are
88finished, the @kbd{C-x )} command (@code{kmacro-end-macro}) terminates the
89definition (without becoming part of it!). For example,
90
91@example
92C-x ( M-f foo C-x )
93@end example
94
95@noindent
96defines a macro to move forward a word and then insert @samp{foo}.
97
98 The macro thus defined can be invoked again with the @kbd{C-x e}
99command (@code{kmacro-end-and-call-macro}), which may be given a
100repeat count as a numeric argument to execute the macro many times.
101If you enter @kbd{C-x e} while defining a macro, the macro is
102terminated and executed immediately.
103
104 After executing the macro with @kbd{C-x e}, you can use @kbd{e}
105repeatedly to immediately repeat the macro one or more times. For example,
106
107@example
108C-x ( xyz C-x e e e
109@end example
110
111@noindent
112inserts @samp{xyzxyzxyzxyz} in the current buffer.
113
114 @kbd{C-x )} can also be given a repeat count as an argument, in
115which case it repeats the macro that many times right after defining
116it, but defining the macro counts as the first repetition (since it is
117executed as you define it). Therefore, giving @kbd{C-x )} an argument
118of 4 executes the macro immediately 3 additional times. An argument
119of zero to @kbd{C-x e} or @kbd{C-x )} means repeat the macro
120indefinitely (until it gets an error or you type @kbd{C-g} or, on
121MS-DOS, @kbd{C-@key{BREAK}}).
122
123@kindex C-x C-k C-s
124@kindex C-x C-k C-k
125Alternatively, you can use @kbd{C-x C-k C-s} to start a keyboard macro,
126and @kbd{C-x C-k C-k...} to end and execute it.
127
128 If you wish to repeat an operation at regularly spaced places in the
129text, define a macro and include as part of the macro the commands to move
130to the next place you want to use it. For example, if you want to change
131each line, you should position point at the start of a line, and define a
132macro to change that line and leave point at the start of the next line.
133Then repeating the macro will operate on successive lines.
134
135 When a command reads an argument with the minibuffer, your
136minibuffer input becomes part of the macro along with the command. So
137when you replay the macro, the command gets the same argument as
138when you entered the macro. For example,
139
140@example
141C-x ( C-a C-@key{SPC} C-n M-w C-x b f o o @key{RET} C-y C-x b @key{RET} C-x )
142@end example
143
144@noindent
145defines a macro that copies the current line into the buffer
146@samp{foo}, then returns to the original buffer.
147
148 You can use function keys in a keyboard macro, just like keyboard
149keys. You can even use mouse events, but be careful about that: when
150the macro replays the mouse event, it uses the original mouse position
151of that event, the position that the mouse had while you were defining
152the macro. The effect of this may be hard to predict. (Using the
153current mouse position would be even less predictable.)
154
155 One thing that doesn't always work well in a keyboard macro is the
156command @kbd{C-M-c} (@code{exit-recursive-edit}). When this command
157exits a recursive edit that started within the macro, it works as you'd
158expect. But if it exits a recursive edit that started before you
159invoked the keyboard macro, it also necessarily exits the keyboard macro
160as part of the process.
161
162 After you have terminated the definition of a keyboard macro, you can add
163to the end of its definition by typing @kbd{C-u C-x (}. This is equivalent
164to plain @kbd{C-x (} followed by retyping the whole definition so far. As
165a consequence it re-executes the macro as previously defined.
166
167 You can also add to the end of the definition of the last keyboard
977d0bea 168macro without re-executing it by typing @kbd{C-u C-u C-x (}.
eef59362
KS
169
170 The variable @code{kmacro-execute-before-append} specifies whether
171a single @kbd{C-u} prefix causes the existing macro to be re-executed
172before appending to it.
173
174@findex apply-macro-to-region-lines
175@kindex C-x C-k r
176 The command @kbd{C-x C-k r} (@code{apply-macro-to-region-lines})
8871f2ba
LT
177repeats the last defined keyboard macro on each line that begins in
178the region. It does this line by line, by moving point to the
eef59362
KS
179beginning of the line and then executing the macro.
180
181@node Keyboard Macro Ring
2935e788 182@section The Keyboard Macro Ring
eef59362
KS
183
184 All defined keyboard macros are recorded in the ``keyboard macro ring'',
185a list of sequences of keys. There is only one keyboard macro ring,
186shared by all buffers.
187
188 All commands which operates on the keyboard macro ring use the
189same @kbd{C-x C-k} prefix. Most of these commands can be executed and
190repeated immediately after each other without repeating the @kbd{C-x
191C-k} prefix. For example,
192
193@example
194C-x C-k C-p C-p C-k C-k C-k C-n C-n C-k C-p C-k C-d
195@end example
196
197@noindent
198will rotate the keyboard macro ring to the ``second previous'' macro,
199execute the resulting head macro three times, rotate back to the
200original head macro, execute that once, rotate to the ``previous''
201macro, execute that, and finally delete it from the macro ring.
202
203@findex kmacro-end-or-call-macro-repeat
204@kindex C-x C-k C-k
205 The command @kbd{C-x C-k C-k} (@code{kmacro-end-or-call-macro-repeat})
206executes the keyboard macro at the head of the macro ring. You can
207repeat the macro immediately by typing another @kbd{C-k}, or you can
208rotate the macro ring immediately by typing @kbd{C-n} or @kbd{C-p}.
209
210@findex kmacro-cycle-ring-next
211@kindex C-x C-k C-n
212@findex kmacro-cycle-ring-previous
213@kindex C-x C-k C-p
214 The commands @kbd{C-x C-k C-n} (@code{kmacro-cycle-ring-next}) and
215@kbd{C-x C-k C-p} (@code{kmacro-cycle-ring-previous}) rotates the
216macro ring, bringing the next or previous keyboard macro to the head
217of the macro ring. The definition of the new head macro is displayed
218in the echo area. You can continue to rotate the macro ring
219immediately by repeating just @kbd{C-n} and @kbd{C-p} until the
220desired macro is at the head of the ring. To execute the new macro
977d0bea 221ring head immediately, just type @kbd{C-k}.
eef59362 222
2935e788
LT
223 Note that Emacs treats the head of the macro ring as the ``last
224defined keyboard macro''. For instance, it is the keyboard macro that
225@kbd{C-x e} will execute.
226
eef59362
KS
227@findex kmacro-view-macro-repeat
228@kindex C-x C-k C-v
229
230 The commands @kbd{C-x C-k C-v} (@code{kmacro-view-macro-repeat})
231displays the last keyboard macro, or when repeated (with @kbd{C-v}),
232it displays the previous macro on the macro ring, just like @kbd{C-x
233C-k C-p}, but without actually rotating the macro ring. If you enter
234@kbd{C-k} immediately after displaying a macro from the ring, that
235macro is executed, but still without altering the macro ring.
236
237 So while e.g. @kbd{C-x C-k C-p C-p C-k C-k} makes the 3rd previous
238macro the current macro and executes it twice, @kbd{C-x C-k C-v C-v
239C-v C-k C-k} will display and execute the 3rd previous macro once and
240then the current macro once.
241
242@findex kmacro-delete-ring-head
243@kindex C-x C-k C-d
244
245 The commands @kbd{C-x C-k C-d} (@code{kmacro-delete-ring-head})
246removes and deletes the macro currently at the head of the macro
247ring. You can use this to delete a macro that didn't work as
248expected, or which you don't need anymore.
249
250@findex kmacro-swap-ring
251@kindex C-x C-k C-t
252
253 The commands @kbd{C-x C-k C-t} (@code{kmacro-swap-ring})
254interchanges the head of the macro ring with the previous element on
255the macro ring.
256
257@findex kmacro-call-ring-2nd-repeat
258@kindex C-x C-k C-l
259
260 The commands @kbd{C-x C-k C-l} (@code{kmacro-call-ring-2nd-repeat})
261executes the previous (rather than the head) element on the macro ring.
262
263@node Keyboard Macro Counter
1dec4064 264@section The Keyboard Macro Counter
eef59362
KS
265
266 Each keyboard macro has an associated counter which is automatically
267incremented on every repetition of the keyboard macro. Normally, the
268macro counter is initialized to 0 when you start defining the macro,
269and incremented by 1 after each insertion of the counter value;
270that is, if you insert the macro counter twice while defining the
2935e788 271macro, the counter will increase by 2 on each repetition of the macro.
eef59362
KS
272
273@findex kmacro-insert-counter
274@kindex C-x C-k C-i
275 The command @kbd{C-x C-k C-i} (@code{kmacro-insert-counter}) inserts
276the current value of the keyboard macro counter and increments the
277counter by 1. You can use a numeric prefix argument to specify a
278different increment. If you specify a @kbd{C-u} prefix, the last
279inserted counter value is repeated and the counter is not incremented.
280For example, if you enter the following sequence while defining a macro
281
282@example
283C-x C-k C-i C-x C-k C-i C-u C-x C-k C-i C-x C-k C-i
284@end example
285
286@noindent
287the text @samp{0112} is inserted in the buffer, and for the first and
288second execution of the macro @samp{3445} and @samp{6778} are
289inserted.
290
291@findex kmacro-set-counter
292@kindex C-x C-k C-c
293 The command @kbd{C-x C-k C-c} (@code{kmacro-set-counter}) prompts
294for the initial value of the keyboard macro counter if you use it
295before you define a keyboard macro. If you use it while defining a
296keyboard macro, you set the macro counter to the same (initial) value
297on each repetition of the macro. If you specify a @kbd{C-u} prefix,
298the counter is reset to the value it had prior to the current
299repetition of the macro (undoing any increments so far in this
300repetition).
301
302@findex kmacro-add-counter
303@kindex C-x C-k C-a
304 The command @kbd{C-x C-k C-a} (@code{kmacro-add-counter}) prompts
305for a value to add to the macro counter.
306
307@findex kmacro-set-format
308@kindex C-x C-k C-f
309 The command @kbd{C-x C-k C-f} (@code{kmacro-set-format}) prompts
310for the format to use when inserting the macro counter. The default
311format is @samp{%d}. If you set the counter format before you define a
312macro, that format is restored before each repetition of the macro.
313Consequently, any changes you make to the macro counter format while
314defining a macro are only active for the rest of the macro.
315
316@node Keyboard Macro Query
317@section Executing Macros with Variations
318
319@kindex C-x q
320@findex kbd-macro-query
321 Using @kbd{C-x q} (@code{kbd-macro-query}), you can get an effect
322similar to that of @code{query-replace}, where the macro asks you each
323time around whether to make a change. While defining the macro,
324type @kbd{C-x q} at the point where you want the query to occur. During
325macro definition, the @kbd{C-x q} does nothing, but when you run the
326macro later, @kbd{C-x q} asks you interactively whether to continue.
327
328 The valid responses when @kbd{C-x q} asks are @key{SPC} (or @kbd{y}),
329@key{DEL} (or @kbd{n}), @key{RET} (or @kbd{q}), @kbd{C-l} and @kbd{C-r}.
330The answers are the same as in @code{query-replace}, though not all of
331the @code{query-replace} options are meaningful.
332
333 These responses include @key{SPC} to continue, and @key{DEL} to skip
334the remainder of this repetition of the macro and start right away with
335the next repetition. @key{RET} means to skip the remainder of this
336repetition and cancel further repetitions. @kbd{C-l} redraws the screen
337and asks you again for a character to say what to do.
338
339 @kbd{C-r} enters a recursive editing level, in which you can perform
340editing which is not part of the macro. When you exit the recursive
341edit using @kbd{C-M-c}, you are asked again how to continue with the
342keyboard macro. If you type a @key{SPC} at this time, the rest of the
343macro definition is executed. It is up to you to leave point and the
344text in a state such that the rest of the macro will do what you
345want.@refill
346
347 @kbd{C-u C-x q}, which is @kbd{C-x q} with a numeric argument,
348performs a completely different function. It enters a recursive edit
349reading input from the keyboard, both when you type it during the
350definition of the macro, and when it is executed from the macro. During
351definition, the editing you do inside the recursive edit does not become
352part of the macro. During macro execution, the recursive edit gives you
353a chance to do some particularized editing on each repetition.
354@xref{Recursive Edit}.
355
356 Another way to vary the behavior of a keyboard macro is to use a
357register as a counter, incrementing it on each repetition of the macro.
358@xref{RegNumbers}.
359
360@node Save Keyboard Macro
361@section Naming and Saving Keyboard Macros
362
363@cindex saving keyboard macros
8e887df4 364@findex kmacro-name-last-macro
eef59362
KS
365@kindex C-x C-k n
366 If you wish to save a keyboard macro for later use, you can give it
8e887df4 367a name using @kbd{C-x C-k n} (@code{kmacro-name-last-macro}).
2935e788
LT
368This reads a name as an argument using the minibuffer and defines that
369name to execute the last keyboard macro, in its current form. (If you
370later add to the definition of this macro, that does not alter the
371name's definition as a macro.) The macro name is a Lisp symbol, and
372defining it in this way makes it a valid command name for calling with
373@kbd{M-x} or for binding a key to with @code{global-set-key}
374(@pxref{Keymaps}). If you specify a name that has a prior definition
375other than a keyboard macro, an error message is shown and nothing is
376changed.
eef59362
KS
377
378@cindex binding keyboard macros
379@findex kmacro-bind-to-key
380@kindex C-x C-k b
2935e788
LT
381 You can also bind the last keyboard macro to a key, using
382@kbd{C-x C-k b} (@code{kmacro-bind-to-key}) followed by the
eef59362
KS
383key sequence you want the keyboard macro to be bound to. You can
384bind to any key sequence in the global keymap, but since most key
385sequences already have other bindings, you should select the key
977d0bea 386sequence carefully. If you try to bind to a key sequence with an
eef59362
KS
387existing binding (in any keymap), you will be asked if you really
388want to replace the existing binding of that key.
389
390To avoid problems caused by overriding existing bindings, the key
391sequences @kbd{C-x C-k 0} through @kbd{C-x C-k 9} and @kbd{C-x C-k A}
392through @kbd{C-x C-k Z} are reserved for your own keyboard macro
393bindings. In fact, to bind to one of these key sequences, you only
394need to type the digit or letter rather than the whole key sequences.
395For example,
396
397@example
398C-x C-k b 4
399@end example
400
401@noindent
402will bind the last keyboard macro to the key sequence @kbd{C-x C-k 4}.
403
404@findex insert-kbd-macro
405 Once a macro has a command name, you can save its definition in a file.
406Then it can be used in another editing session. First, visit the file
407you want to save the definition in. Then use this command:
408
409@example
410M-x insert-kbd-macro @key{RET} @var{macroname} @key{RET}
411@end example
412
413@noindent
414This inserts some Lisp code that, when executed later, will define the
415same macro with the same definition it has now. (You need not
416understand Lisp code to do this, because @code{insert-kbd-macro} writes
417the Lisp code for you.) Then save the file. You can load the file
418later with @code{load-file} (@pxref{Lisp Libraries}). If the file you
419save in is your init file @file{~/.emacs} (@pxref{Init File}) then the
420macro will be defined each time you run Emacs.
421
422 If you give @code{insert-kbd-macro} a numeric argument, it makes
2935e788
LT
423additional Lisp code to record the keys (if any) that you have bound
424to @var{macroname}, so that the macro will be reassigned the same keys
425when you load the file.
eef59362
KS
426
427@node Edit Keyboard Macro
2935e788 428@section Editing a Keyboard Macro
eef59362
KS
429
430@findex kmacro-edit-macro
431@kindex C-x C-k C-e
432@kindex C-x C-k RET
433 You can edit the last keyboard macro by typing @kbd{C-x C-k C-e} or
434@kbd{C-x C-k RET} (@code{kmacro-edit-macro}). This formats the macro
435definition in a buffer and enters a specialized major mode for editing
436it. Type @kbd{C-h m} once in that buffer to display details of how to
437edit the macro. When you are finished editing, type @kbd{C-c C-c}.
438
439@findex edit-kbd-macro
440@kindex C-x C-k e
441 You can edit a named keyboard macro or a macro bound to a key by typing
442@kbd{C-x C-k e} (@code{edit-kbd-macro}). Follow that with the
443keyboard input that you would use to invoke the macro---@kbd{C-x e} or
444@kbd{M-x @var{name}} or some other key sequence.
445
446@findex kmacro-edit-lossage
447@kindex C-x C-k l
448 You can edit the last 100 keystrokes as a macro by typing
449@kbd{C-x C-k l} (@code{kmacro-edit-lossage}).
450
451@node Keyboard Macro Step-Edit
1dec4064 452@section Stepwise Editing a Keyboard Macro
eef59362
KS
453
454@findex kmacro-step-edit-macro
455@kindex C-x C-k SPC
456 You can interactively and stepwise replay and edit the last keyboard
977d0bea 457macro one command at a time by typing @kbd{C-x C-k SPC}
eef59362
KS
458(@code{kmacro-step-edit-macro}). Unless you quit the macro using
459@kbd{q} or @kbd{C-g}, the edited macro replaces the last macro on the
460macro ring.
461
462This shows the last macro in the minibuffer together with the first
463(or next) command to be executed, and prompts you for an action.
464You can enter @kbd{?} to get a command summary.
465
466The following commands are available in the step-edit mode and relate
467to the first (or current) command in the keyboard macro:
468
469@itemize @bullet{}
470@item
471@kbd{SPC} and @kbd{y} execute the current command, and advance to the
472next command in the keyboard macro.
473@item
474@kbd{n}, @kbd{d}, and @kbd{DEL} skip and delete the current command.
475@item
476@kbd{f} skips the current command in this execution of the keyboard
477macro, but doesn't delete it from the macro.
478@item
479@kbd{TAB} executes the current command, as well as all similar
480commands immediately following the current command; for example, TAB
481may be used to insert a sequence of characters (corresponding to a
482sequence of @code{self-insert-command} commands).
483@item
484@kbd{c} continues execution (without further editing) until the end of
485the keyboard macro. If execution terminates normally, the edited
486macro replaces the original keyboard macro.
487@item
488@kbd{C-k} skips and deletes the rest of the keyboard macro,
489terminates step-editing, and replaces the original keyboard macro
490with the edited macro.
491@item
492@kbd{q} and @kbd{C-g} cancels the step-editing of the keyboard macro;
493discarding any changes made to the keyboard macro.
494@item
495@kbd{i KEY... C-j} reads and executes a series of key sequences (not
496including the final @kbd{C-j}), and inserts them before the current
497command in the keyboard macro, without advancing over the current
498command.
499@item
500@kbd{I KEY...} reads one key sequence, executes it, and inserts it
501before the current command in the keyboard macro, without advancing
502over the current command.
503@item
504@kbd{r KEY... C-j} reads and executes a series of key sequences (not
505including the final @kbd{C-j}), and replaces the current command in
506the keyboard macro with them, advancing over the inserted key
507sequences.
508@item
509@kbd{R KEY...} reads one key sequence, executes it, and replaces the
510current command in the keyboard macro with that key sequence,
511advancing over the inserted key sequence.
512@item
513@kbd{a KEY... C-j} executes the current command, then reads and
514executes a series of key sequences (not including the final
515@kbd{C-j}), and inserts them after the current command in the keyboard
516macro; it then advances over the current command and the inserted key
517sequences.
518@item
519@kbd{A KEY... C-j} executes the rest of the commands in the keyboard
520macro, then reads and executes a series of key sequences (not
521including the final @kbd{C-j}), and appends them at the end of the
522keyboard macro; it then terminates the step-editing and replaces the
523original keyboard macro with the edited macro.
524@end itemize
ab5796a9
MB
525
526@ignore
527 arch-tag: c1b0dd3b-3159-4c08-928f-52e763953e9c
528@end ignore