How do I increment all numbers in a visual selection by the same amount?
Answer
{Visual}<C-a>
Explanation
In Visual mode, pressing <C-a> increments every number within the selection by 1 (or by a given [count]). Unlike normal mode <C-a> which only affects the number under the cursor, the visual variant targets all numbers in the selected text simultaneously — and all receive the same increment, keeping their relative differences intact.
This is the key distinction from g<C-a>, which creates a sequential series (each number increases by an additional 1). Here, every number shifts by the exact same delta.
How it works
- Enter Visual mode (
v,V, or<C-v>) and select the text containing the numbers - Press
<C-a>to add 1 to each number, or[count]<C-a>to add a specific value - Use
<C-x>to decrement instead
Example
Given this CSS fragment, cursor anywhere on the block:
padding: 4px;
margin: 8px;
border-radius: 2px;
Select all three lines with V2j, then press 4<C-a> (add 4 to each):
padding: 8px;
margin: 12px;
border-radius: 6px;
Every value increases by exactly 4 — no re-typing required.
Tips
- Use
g<C-a>instead when you want sequential numbering (1, 2, 3…) - Works on octal and hex numbers too if
'nrformats'includesoctalorhex - Combine with a macro: record the visual select +
<C-a>for repeatable batch edits