How do I use a macro to automatically increment a number on each line?
Answer
qa<C-a>jq
Explanation
By recording a one-step macro that increments a number and moves down a line, you can bulk-apply <C-a> across as many lines as needed with a single count. This is perfect when you have a column of identical values that each need to be bumped by 1 — or combined with a multiplier for larger increments.
How it works
qa— start recording into registera<C-a>— increment the first number on the current line by 1j— advance to the next lineq— stop recording{N}@a— replay the macroNtimes
Vim stops automatically when j has nowhere to go (e.g., end of file), so the macro is self-limiting.
Example
Given:
timeout = 10
retries = 10
max_conn = 10
Place the cursor on the first 10, record qa<C-a>jq, then run 2@a:
timeout = 11
retries = 11
max_conn = 11
Tips
- Swap
<C-a>for<C-x>to decrement instead - Prefix a count before
<C-a>to step by more than 1:qa5<C-a>jqincrements by 5 each line - To create a sequential series where each line is one higher than the previous (1, 2, 3…), use
g<C-a>on a visual selection — that is a different use case <C-a>respects:set nrformats: hex (0xff) and octal values are also incremented correctly