Next: , Previous: Keyboard Macro Ring, Up: Keyboard Macros


22.3 The Keyboard Macro Counter

Each keyboard macro has an associated counter. Normally, the macro counter is initialized to 0 when you start defining the macro, and incremented by 1 after each insertion of the counter value; that is, if you insert the macro counter twice while defining the macro, the counter will increase by 2 on each repetition of the macro.

The command C-x C-k C-i (kmacro-insert-counter) inserts the current value of the keyboard macro counter and increments the counter by 1. You can use a numeric prefix argument to specify a different increment. If you just specify a C-u prefix, the last inserted counter value is repeated and the counter is not incremented. For example, if you enter the following sequence while defining a macro

     C-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

the text ‘0112’ is inserted in the buffer, and for the first and second execution of the macro ‘3445’ and ‘6778’ are inserted.

This command usually only makes sense while defining a keyboard macro. But its behavior when no keyboard macro is being defined or executed is predictable: it inserts and increments the counter of the head of the keyboard macro ring.

The command C-x C-k C-c (kmacro-set-counter) prompts for the initial value of the keyboard macro counter if you use it before you define a keyboard macro. If you use it before executing a keyboard macro, it resets that macro's counter. If you use it while defining a keyboard macro, then the macro counter gets reset to that same value on each repetition of the macro. Rather than having the command prompt for a value, you can also specify the value with a numeric prefix argument. If you just specify a C-u prefix, the counter is reset to the value it had prior to the current repetition of the macro (undoing any increments so far in this repetition). If you just specify a C-u prefix while no macro is being defined or executed, then the new value of the counter is essentially unpredictable.

The command C-x C-k C-a (kmacro-add-counter) prompts for a value to add to the macro counter. You can also specify the value with a numeric prefix argument. If you just specify a C-u prefix, the counter is reset to the last value inserted by any keyboard macro. Usually, this will only make sense if that value was inserted during the current macro definition or repetition.

This command normally only makes sense while defining a keyboard macro. But its behavior when no keyboard macro is being defined or executed is predictable: it affects the counter of the head of the keyboard macro ring.

The command C-x C-k C-f (kmacro-set-format) prompts for the format to use when inserting the macro counter. The default format is ‘%d’, which means to insert the number in decimal without any padding. You can exit with empty minibuffer to reset the format to this default. You can specify any format string that the format function accepts and that makes sense with a single integer extra argument (see Formatting Strings). Do not put the format string inside double quotes when you insert it in the minibuffer.

If you use this command while no keyboard macro is being defined or executed, the new format affects all subsequent macro definitions. Existing macros continue to use the format in effect when they were defined. If you set the format while defining a keyboard macro, this affects the macro being defined from that point on, but it does not affect subsequent macros. Execution of the macro will, at each step, use the format in effect at that step during its definition. Changes to the macro format during execution of a macro, like the corresponding changes during its definition, have no effect on subsequent macros.

The format set by C-x C-k C-f does not affect insertion of numbers stored in registers.