Search code examples
emacs

Emacs macro to generate a sequence?


I was reading this article on how to create vi macros: vi sequence macro and I saw one useful macro to create a sequence in the buffer.

1.
2.
...
100.

In vi, the trick is that one can hit ctrl-a over a number and it will add one to it, so this made the macro easy. I want to be able to do the same thing in emacs.


Solution

  • If you are using Emacs 23 (and maybe 22?), use kmacro-insert-counter which is bound to C-x C-k TAB by default. So for your example, you'd do:

    C-x ( C-x C-k TAB . RET C-x )

    So start macro, insert counter followed by '.', newline, end macro. Then C-x e e e e e e e etc. Or M-1 0 0 C-x e to get 100 of them.

    EDIT:

    Forgot to mention you can set the counter to an initial value also. For example to start at 1 instead of 0 do M-1 C-x C-k C-c.

    And if you don't want the counter to increment at a particular point, prefix it with C-u. Of course the keystrokes are getting a bit ridiculous at this point, so I usually bind a key to insert-but-don't-increment.