How do I add or subtract a specific number to a number under the cursor?
Answer
{count}<C-a> / {count}<C-x>
Explanation
While <C-a> increments and <C-x> decrements a number by 1, you can prefix either with a count to add or subtract a specific amount. This is incredibly useful when adjusting port numbers, array indices, CSS values, or any numeric constant without mental arithmetic.
How it works
100<C-a>— adds 100 to the number under (or after) the cursor5<C-x>— subtracts 5 from the number under the cursor- Vim finds the first number on or after the cursor position on the current line
- Works with decimal, hex (
0x), octal (0), and binary (0b) depending onnrformats
Example
Before (cursor on the line):
server.port = 3000
After pressing 5000<C-a>:
server.port = 8000
Tips
- Combine with
.(dot) to repeat:100<C-a>then press.to add another 100 - Use with
<C-x>the same way:10<C-x>subtracts 10 - In visual mode,
{count}<C-a>increments numbers on all selected lines by the same amount - For sequential numbering across lines, use
g<C-a>instead