vimtricks.wiki Concise Vim tricks, one at a time.

How do I create an incrementing number sequence across multiple lines?

Answer

<C-v>jjjg<C-a>

Explanation

Selecting a column of identical numbers with visual block mode and pressing g<C-a> turns them into an incrementing sequence.

How it works

  1. Start with identical numbers in a column
  2. Select them with <C-v> and j motions
  3. Press g<C-a> to increment each line progressively
  4. First selected number gets +1, second +2, etc.

Example

item[0] = a;
item[0] = b;
item[0] = c;
item[0] = d;

Select the column of 0s, press g<C-a>:

item[1] = a;
item[2] = b;
item[3] = c;
item[4] = d;

Tips

  • g<C-x> creates a decrementing sequence
  • Regular <C-a> adds the same amount to all selected numbers
  • Useful for array indices, port numbers, enum values
  • Start with 0 to get a 1-based sequence with g<C-a>

Next

How do you yank a single word into a named register?