How do I increment and decrement hexadecimal numbers with Ctrl-A in Vim?
Answer
:set nrformats+=hex
Explanation
Vim's <C-a> and <C-x> commands increment and decrement the number under the cursor. By default, nrformats includes bin (binary) and octal but not hex. Adding hex to nrformats lets Vim recognise 0x-prefixed values and increment them as hexadecimal.
How it works
nrformatsis a comma-separated list telling Vim which numeric formats to recognise for<C-a>/<C-x>- Default value:
bin,octal(orbin,octal,unsignedin newer versions) - Adding
hexenables recognition of0x…and0X…literals
Add to your vimrc to make it permanent:
set nrformats+=hex
Example
With the cursor on 0x1f:
before: 0x1f
Press <C-a> once:
after: 0x20
Press <C-x> to decrement back:
after: 0x1f
Case is preserved: 0xFF increments to 0x100.
Tips
- To remove octal recognition (so
007is not treated as octal and accidentally jumps to010), use:set nrformats-=octal - Combine with
<C-a>in Visual block mode (g<C-a>) to increment multiple hex values sequentially — useful when patching memory addresses or colour codes nrformats=alphaalso lets you increment letters (a→b), though that is a separate feature