Vim: Cookbook
A collection of cool Vim tricks I have picked up over the years.
Increment a list of numbers
c+a
will increment the next number on the current line from the cursor
position and looking to the right.
c+x
will decrement the next number, although that’s not working for me in my
current configuration (some plugin has remapped that I suspect).
This is pretty cool by itself, but where it gets really useful is when you have a list of numbers on different lines that you want to keep incrementing. The case that I often use is constructing an array.
You might have something like this:
base[0] = "blah"
base[0] = "blah"
base[0] = "blah"
base[0] = "blah"
Where you have repeated the first line in readiness to fill in the array values. Often you would jump to each line and update the number.
But, you could instead visually highly the lines and then use the g<C-a>
command. This will increment the array index values as you expect. It’s like
magic when you use it.
If you base index is not zero (maybe you copied it from somewhere else) this substitute command will make all those indexes zero.
:23,45s/[0-9]\+/0/