Counting with `g` followed by increment/decrement
In Vim, you can type ctrl-a
or ctrl-x
to increment or decrement the next
number in the line. For example, suppose you have the following line:
1. Do something
If you put the cursor on number one and type ctrl-a
repeated times, you notice
the number will increase to 2, 3, etc. Similarly, if you type ctrl-x
, it will decrease
going into negative numbers if you do it enough times.
Let’s say you want to create a numbered list. You have a list of people:
Ana
Daniel
Douglas
Fabiane
Marina
Letícia
Pedro
You used the tip about :normal
command to insert a number
to insert a number at the beginning of the lines:
0. Ana
0. Daniel
0. Douglas
0. Fabiane
0. Marina
0. Letícia
0. Pedro
Now you want to change those noughts to 1-7. You can go to the first line, type
ctrl-a
, go to the second line, type ctrl-a
twice, go to the third line,
type ctrl-a
three times and so on. But that is annoying. Instead, visually
select all numbers (using ctrl-v
), then type g
followed by ctrl-a
. It
will automatically change the first line to 1, the second to 2, and so on.
1. Ana
2. Daniel
3. Douglas
4. Fabiane
5. Marina
6. Letícia
7. Pedro
This tip also works with ctrl-x
to decrease the numbers.
Photo by the author