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

How do I use a macro to generate a numbered sequence in Vim?

Answer

qa Yp <C-a> q

Explanation

By combining a macro with <C-a> (increment number), you can quickly generate numbered sequences. Record a macro that duplicates a line and increments its number, then replay it as many times as needed to build your list.

How it works

  • qa starts recording into register a
  • Yp yanks the current line and pastes it below
  • <C-a> increments the first number found on the line
  • q stops recording
  • 10@a replays the macro 10 times, creating a sequence

Example

Start with this line:

item 1

Record with qaYp<C-a>q, then run 8@a:

item 1
item 2
item 3
item 4
item 5
item 6
item 7
item 8
item 9
item 10

Tips

  • Use <C-x> instead of <C-a> for a decrementing sequence
  • For zero-padded numbers, start with 01 and set :set nrformats+=octal off — Vim handles leading zeros as decimal by default in recent versions
  • This pattern works for any repetitive task that involves incrementing: port numbers, array indices, test case IDs

Next

How do I return to normal mode from absolutely any mode in Vim?