Incrementing numbers in a macro

15 views
Skip to first unread message

aditya siram

unread,
Oct 15, 2012, 11:03:54 AM10/15/12
to vim-...@googlegroups.com
Hi all,
Is there a way of maintaining a incrementing counter in a macro in vanilla Vim? By vanilla I mean the Vim that ships with OSX and an empty config file. Googling seems to suggest the answer is no.
-deech

Matt Follett

unread,
Oct 15, 2012, 12:08:19 PM10/15/12
to vim-...@googlegroups.com
There are better ways to do this, but here is a starter. Put this in register a:
ifoo[ ^Rb] = blah^M^[:let @b=@b+1
prime register b with the value you want to start at (e.g. 0). Then just call reg a as a macro.

Other solutions would involve replacing register b with a variable. I'd also be curious to see this done in a single register by printing the string then replacing the macro with a version that has the number incremented. Anyone want to take a shot at these or another approach?

Thanks,
Matt Follett

aditya siram

unread,
Oct 15, 2012, 12:37:52 PM10/15/12
to vim-...@googlegroups.com
I found a really stupid solution. Ignoring that there are better ways to do this let's say that I want to create a macro that takes
0
and after running it 4 times produces:
0
1
2
3
4

Here are the steps I followed:
-  type '0'
- start a macro in register 'q' with 'qq'
- With point on '0' I do a yw,
- Shift-o to get to the next line
- 'Control-o p' to paste the '0'
- Place point on the new '0'
- Control-A to increment to '1'
-  yw to replace the '0' in the unnamed register with '1',
- 'q' to stop the macro
- replay the macro 4 times with '@q'

-deech
Reply all
Reply to author
Forward
0 new messages