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

How do I create an incrementing number sequence across multiple lines using visual block mode?

Answer

g (in visual mode)

Explanation

With a visual block selection over multiple identical numbers, g<C-a> increments each one by a progressively increasing amount — turning a column of 0s into 1, 2, 3, 4, .... This is Vim's built-in way to generate numbered sequences without macros or external tools.

How it works

  1. Type the same number (e.g., 0) on consecutive lines
  2. Select all of them with <C-v> (visual block)
  3. Press g<C-a> — each number increments by one more than the previous

Regular <C-a> vs g<C-a>

Command Effect on selected numbers
<C-a> Adds 1 to ALL selected numbers (all become 1)
g<C-a> Adds 1 to first, 2 to second, 3 to third, etc.
3g<C-a> Adds 3, 6, 9, 12, ... (step of 3)

Example

Start with:

0. Item
0. Item
0. Item
0. Item

Select the 0 column with <C-v>3j, then press g<C-a>:

1. Item
2. Item
3. Item
4. Item

With 5g<C-a> instead:

5. Item
10. Item
15. Item
20. Item

Tips

  • g<C-x> does the same but decrementing (1, 0, -1, -2, ...)
  • Works with hex (0x), octal (0), and binary (0b) numbers when nrformats is set appropriately
  • The starting value matters: if all lines start at 5, g<C-a> produces 6, 7, 8, 9, ...
  • Requires Vim 8.0+ — older versions only have <C-a> without the g prefix variant

Next

How do I run a search and replace only within a visually selected region?