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

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 with 0x or 0X are treated as hex.
  • :set nrformats+=bin — adds binary recognition. Numbers prefixed with 0b or 0B are 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 vimrc to make it permanent: set nrformats=bin,hex
  • The default value of nrformats is bin,octal,hex in Neovim, but in Vim it is bin,octal — so Vim users need to add hex explicitly
  • You can also remove octal if you never work with octal numbers and want to avoid 007 being treated as octal: :set nrformats-=octal
  • Combine with visual block mode and g<C-a> to increment sequences of hex addresses

Next

How do I return to normal mode from absolutely any mode in Vim?