kmacro.texi 24 KB

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