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

How do I create incrementing number sequences in Vim?

Answer

g<C-a>

Explanation

The g<C-a> command increments numbers across a visual selection so that each subsequent line gets a progressively higher value. This turns a column of identical numbers into a sequential list, which is incredibly useful for creating numbered lists, array indices, or test data.

How it works

  • First, create a column of identical numbers (e.g., by duplicating a line with 0 on it)
  • Select all the lines with visual line mode (V) or visual block mode (<C-v>)
  • Press g<C-a> to increment each number progressively: the first stays the same, the second gets +1, the third gets +2, and so on

Example

Start with these lines (created by typing 0 then duplicating with yy4p):

0
0
0
0
0

Select all five lines with ggVG, then press g<C-a>. The result:

1
2
3
4
5

Each number is incremented by one more than the previous line.

Compared to regular Ctrl-a

  • <C-a> in normal mode increments the number under the cursor by 1
  • <C-x> in normal mode decrements the number under the cursor by 1
  • g<C-a> in visual mode creates a sequential increment across selected lines
  • g<C-x> in visual mode creates a sequential decrement across selected lines

Tips

  • Use 5<C-a> to increment a number by 5 instead of 1
  • Use <C-x> to decrement a number by 1
  • Use g<C-x> on a visual selection to create a decreasing sequence
  • Vim can increment octal, hex, and binary numbers too — control this with :set nrformats=alpha,hex,bin
  • This feature requires Vim 8+ or Neovim — older Vim versions do not support g<C-a> in visual mode

Next

How do I edit multiple lines at once using multiple cursors in Vim?