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
qastarts recording into registeraYpyanks the current line and pastes it below<C-a>increments the first number found on the lineqstops recording10@areplays 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
01and set:set nrformats+=octaloff — 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