How do I prevent Vim from treating leading-zero numbers as octal when incrementing with Ctrl-A?
Answer
set nrformats-=octal
Explanation
By default, Vim treats numbers prefixed with a leading zero (like 007) as octal when you use <C-a> or <C-x> to increment or decrement them. This means pressing <C-a> on 007 yields 010 (octal 8) rather than the intuitive 008. Setting set nrformats-=octal removes octal from the list of recognized formats, ensuring all decimal-looking numbers increment in base 10.
How it works
nrformatsis a comma-separated list of formats that<C-a>and<C-x>recognize- Default Vim value:
bin,octal,hex - Default Neovim value:
bin,hex(octal is already excluded) nrformats-=octalremovesoctalfrom the list without affecting binary (0b1010) or hex (0xff) support- The
-=operator removes an item from a comma-separated option
Example
With the default setting in Vim, placing the cursor on 007 and pressing <C-a> gives:
010
After adding set nrformats-=octal to your .vimrc, the same operation gives:
008
Tips
- Add to your
.vimrcto make it permanent:set nrformats-=octal - Verify the current value with
:set nrformats? - Combine with
set nrformats+=alphato also increment alphabetical characters with<C-a> - This only affects Vim; Neovim users get sane decimal behavior out of the box