Is there a Vim-eqsue way to sequentially copy numbers?

50 Views Asked by At

So I am trying to see if there is a Vim command or algorithm to where I can sequentially copy numbers.

So essentially, if I have variables foo1 to foo10, is there a way to go from just:

foo1

to

foo1
foo1
foo1
foo1
foo1
foo1
foo1
foo1
foo1
foo1
foo1

to this:

foo1
foo2
foo3
foo4
foo5
foo6
foo7
foo8
foo9
foo10

or otherwise? Thank you.

1

There are 1 best solutions below

2
Amadan On BEST ANSWER

Yes. {Visual}g CTRL-A:

Add [count] to the number or alphabetic character in the highlighted text. If several lines are highlighted, each one will be incremented by an additional [count] (so effectively creating a [count] incrementing sequence). For Example, if you have this list of numbers:

1. 
1. 
1. 
1. 

Move to the second "1." and Visually select three lines, pressing gCTRL-A results in:

1. 
2. 
3. 
4. 

However (tangential as it may be), while there is certainly a use case for this, "have variables foo1 to foo10" is almost always a code smell. Most if not all programming languages have collection types like arrays/lists and dictionaries/hashes that address the need to have multiple grouped values.