Vim tip 1: increment/decrement numbers
Did you know that you can easily increment or decrement a number in Vim?
- Ctrl+a will increment the number under the cursor or the first occurrence of a number to the right of the cursor
- Ctrl+x will decrement the number under the cursor or the first occurrence of a number to the right of the cursor
You can also provide a count prefix:
- 3 followed by Ctrl+a will add
3
- 1000 followed by Ctrl+x will subtract
1000
Numbers prefixed with 0
, 0x
and 0b
will be treated as octal, hexadecimal and binary respectively. You can also use uppercase for x
and b
. What if you want numbers prefixed with 0
to be treated as decimal? You can use the nrformats
setting as shown below:
set nrformats-=octal
Decimal numbers prefixed with -
will be treated as negative numbers. For example, using Ctrl+a on -100
will give you -99
. While this is handy, this trips me up often when dealing with date formats like 2021-12-08.
See :h ctrl-a, :h ctrl-x and :h nrformats for documentation.
Video demo:
See also my curated list of resources for Vim and Vim Reference Guide.