File: kmacro.el.html

The kmacro package provides the user interface to Emacs' basic keyboard macro functionality. With kmacro, two function keys are dedicated to keyboard macros, by default F3 and F4.

Note: The traditional bindings C-x (, C-x ), and C-x e are still supported, but for some users these bindings are too hard to type to be really useful for doing small repeated tasks.

To start defining a keyboard macro, use F3. To end the macro, use F4, and to call the macro also use F4. This makes it very easy to repeat a macro immediately after defining it.

You can call the macro repeatedly by pressing F4 multiple times, or you can give a numeric prefix argument specifying the number of times to repeat the macro. Macro execution automatically terminates when point reaches the end of the buffer or if an error is signaled by ringing the bell.

When you define a macro with F3/F4, it is automatically added to the head of the "keyboard macro ring", and F4 actually executes the first element of the macro ring.

Note: an empty macro is never added to the macro ring.

You can execute the second element on the macro ring with C-u F4 or C-x C-k C-l, you can use C-x C-k C-p and C-x C-k C-n to cycle through the macro ring, and you can swap the first and second elements with C-x C-k C-t. To delete the first element in the macro ring, use C-x C-k C-d.

You can also use C-x C-k C-s to start a macro, and C-x C-k C-k to end it; then use C-k to execute it immediately, or C-x C-k C-k to execute it later.

In general, immediately after using C-x C-k followed by one of C-k, C-l, C-p, or C-n, you can further cycle the macro ring using C-p or C-n, execute the first or second macro using C-k or C-l, delete the head macro with C-d, or edit the current macro with C-e without repeating the C-x C-k prefix.

If you enter F3 while defining the macro, the numeric value of kmacro-counter is inserted using the kmacro-counter-format, and kmacro-counter is incremented by 1 (or the numeric prefix value of F3).

The initial value of kmacro-counter is 0, or the numeric prefix value given to F3 when starting the macro.

Now, each time you call the macro using F4, the current value of kmacro-counter is inserted and incremented, making it easy to insert incremental numbers in the buffer.

Example:

The following sequence: M-5 F3 x M-2 F3 y F4 F4 F4 F4
inserts the following string: x5yx7yx9yx11y

A macro can also be called using a mouse click, default S-mouse-3. This calls the macro at the point where you click the mouse.

You can edit the last macro using C-x C-k C-e.

You can append to the last macro using C-u F3.

You can set the macro counter using C-x C-k C-c, add to it using C-x C-k C-a, and you can set the macro counter format with C-x C-k C-f.

The following key bindings are performed:

          Normal While defining macro
          --------------------------- ------------------------------
 f3 Define macro Insert current counter value
          Prefix arg specifies initial and increase counter by prefix
          counter value (default 0) (default increment: 1)

 C-u f3 APPENDs to last macro

 f4 Call last macro End macro
          Prefix arg specifies number
          of times to execute macro.

 C-u f4 Swap last and head of macro ring.

 S-mouse-3 Set point at click and End macro and execute macro at
            execute last macro. click.

Defined variables (16)

kmacro-call-mouse-eventThe mouse event used by kmacro to call a macro.
kmacro-call-repeat-keyAllow repeating macro call using last key or a specific key.
kmacro-call-repeat-with-argRepeat macro call with original arg when non-nil; repeat once if nil.
kmacro-counterCurrent keyboard macro counter.
kmacro-counter-formatCurrent keyboard macro counter format.
kmacro-counter-format-startMacro format at start of macro execution.
kmacro-counter-value-startMacro counter at start of macro execution.
kmacro-execute-before-appendControls whether appending to a macro starts by executing the macro.
kmacro-initial-counter-valueInitial counter value for the next keyboard macro to be defined.
kmacro-keymapKeymap for keyboard macro commands.
kmacro-last-counterLast counter inserted by key macro.
kmacro-repeat-no-prefixAllow repeating certain macro commands without entering the C-x C-k prefix.
kmacro-ringThe keyboard macro ring.
kmacro-ring-maxMaximum number of keyboard macros to save in macro ring.
kmacro-step-edit-mapKeymap that defines the responses to questions in ‘kmacro-step-edit-macro’.
kmacro-step-edit-mini-window-heightOverride ‘max-mini-window-height’ when step edit keyboard macro.

Defined functions (52)

copy-kmacro-register(ARG)
kmacro-add-counter(ARG)
kmacro-bind-to-key(ARG)
kmacro-call-macro(ARG &optional NO-REPEAT END-MACRO MACRO)
kmacro-call-ring-2nd(ARG)
kmacro-call-ring-2nd-repeat(ARG)
kmacro-cycle-ring-next(&optional ARG)
kmacro-cycle-ring-previous(&optional ARG)
kmacro-delete-ring-head(&optional ARG)
kmacro-display(MACRO &optional TRUNC DESCR EMPTY)
kmacro-display-counter(&optional VALUE)
kmacro-edit-lossage()
kmacro-edit-macro(&optional ARG)
kmacro-edit-macro-repeat(&optional ARG)
kmacro-end-and-call-macro(ARG &optional NO-REPEAT)
kmacro-end-call-mouse(EVENT)
kmacro-end-macro(ARG)
kmacro-end-or-call-macro(ARG &optional NO-REPEAT)
kmacro-end-or-call-macro-repeat(ARG)
kmacro-exec-ring-item(ITEM ARG)
kmacro-extract-lambda(MAC)
kmacro-insert-counter(ARG)
kmacro-keyboard-macro-p(SYMBOL)
kmacro-lambda-form(MAC &optional COUNTER FORMAT)
kmacro-loop-setup-function()
kmacro-make-register(MACRO)
kmacro-make-register--cmacro(CL-WHOLE-ARG MACRO)
kmacro-name-last-macro(SYMBOL)
kmacro-p(ARG1)
kmacro-pop-ring(&optional RAW)
kmacro-pop-ring1(&optional RAW)
kmacro-push-ring(&optional ELT)
kmacro-redisplay()
kmacro-register-macro(CL-X)
kmacro-register-macro--cmacro(CL-WHOLE-ARG CL-X)
kmacro-register-p(CL-X)
kmacro-register-p--cmacro(CL-WHOLE-ARG CL-X)
kmacro-repeat-on-last-key(KEYS)
kmacro-ring-empty-p(&optional NONE)
kmacro-ring-head()
kmacro-set-counter(ARG)
kmacro-set-format(FORMAT)
kmacro-split-ring-element(ELT)
kmacro-start-macro(ARG)
kmacro-start-macro-or-insert-counter(ARG)
kmacro-step-edit-macro()
kmacro-step-edit-prompt(MACRO INDEX)
kmacro-swap-ring()
kmacro-to-register(R)
kmacro-view-macro(&optional ARG)
kmacro-view-macro-repeat(&optional ARG)
kmacro-view-ring-2nd()

Defined faces (0)