tags:

views:

517

answers:

6

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.

+1  A: 

Here is an extension that may help.

nedblorf
+7  A: 

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.

scottfrazer
+2  A: 

Beside scottfrazer's answer, there is another way to create a sequence of numbers with CUA mode which may help you a lot when editing existing content. See Mark Mansour's screencast on Emacs Column Editing from position 2:30.

Török Gábor
+2  A: 

Emacs 23 supports elisp snippets in the replacement text of replace-regexp.

I frequently define keyboard macros that follow this pattern:

  • Copy a block of text
  • Navigate to a number that I want to increment in the copied block of text with isearch
  • Activate the mark and move the point to define a region encompassing the number
  • M-x replace-regexp
  • At the "Replace regexp" prompt, enter \([0-9]+\) to capture a group of one or more digits
  • At the "Replace regexp ([0-9]+) with:" prompt, enter \,(1+ \#1), where , indicates that an elisp form to substitute follows, 1+ is an increment function, and \#1 is the first captured match text, interpreted as a number.

After taking a minute to define the keyboard macro, this allows me to have almost the convenience of cutting and pasting to generate lots of blocks of almost-identical code, such as for case statements.

Note that this technique can be easily adapted to e.g. double numbers (\,(* 2 \#1)) or whatever. You can even use it to substitute the next element in an arbitrary sequence by using a combination of 'position and 'nth, but I won't go into that now :).

jlf
thanks for demonstrating how to use replace-regexp to substitute a counter for text
Noah Sussman
+1  A: 

There's also:

C-u M-! jot -s '.C-q C-j' 10

It's not pure elisp, but has the same effect. You could write a named macro to run it for you.

Just Some Guy
where can I find the jot command?
'jot' and 'seq' do the same thing for these purposes. If you're on Unix, you almost surely have at least one of them installed.q
Just Some Guy
+9  A: 

Those who feel there are too many tricks to memorize might find acquiring some elisp more profitable:

M-: (dotimes (i 20) (insert (format "%2d.\n" (1+ i))))
huaiyuan
+1 for mentioning `M-:` (`eval-expression`), I never heard about it.
Török Gábor