vimtricks.wiki Concise Vim tricks, one at a time.

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

  • nrformats is a comma-separated list telling Vim which numeric formats to recognise for <C-a> / <C-x>
  • Default value: bin,octal (or bin,octal,unsigned in newer versions)
  • Adding hex enables recognition of 0x… and 0X… 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 007 is not treated as octal and accidentally jumps to 010), 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=alpha also lets you increment letters (ab), though that is a separate feature

Next

How do I create command-line aliases so that typos like W are automatically corrected to w?