How do I increment or decrement hexadecimal and binary numbers with Ctrl-A and Ctrl-X?
Answer
:set nrformats+=hex,bin
Explanation
By default, Vim's <C-a> and <C-x> only increment and decrement decimal numbers. If you work with hex values like 0x1F or binary literals like 0b1010, these get ignored or misinterpreted. Setting nrformats tells Vim to recognize additional number formats so increment and decrement work correctly on them.
How it works
:set nrformats+=hex— adds hexadecimal recognition. Numbers prefixed with0xor0Xare treated as hex.:set nrformats+=bin— adds binary recognition. Numbers prefixed with0bor0Bare treated as binary.- Once set,
<C-a>and<C-x>work natively on these formats, carrying and borrowing digits correctly.
Example
With nrformats+=hex,bin set, place your cursor on the number and press <C-a>:
Before: 0xFF
After: 0x100
Before: 0b1011
After: 0b1100
Tips
- Add this to your
vimrcto make it permanent:set nrformats=bin,hex - The default value of
nrformatsisbin,octal,hexin Neovim, but in Vim it isbin,octal— so Vim users need to addhexexplicitly - You can also remove
octalif you never work with octal numbers and want to avoid007being treated as octal::set nrformats-=octal - Combine with visual block mode and
g<C-a>to increment sequences of hex addresses