How do I stop Vim from treating numbers with leading zeros as octal when using Ctrl-A or Ctrl-X?
Answer
:set nrformats-=octal
Explanation
Vim's <C-a> and <C-x> increment and decrement numbers under the cursor. By default, the nrformats option includes octal, which causes any integer with a leading zero to be interpreted as an octal literal. This means pressing <C-a> on 007 produces 010 (eight in decimal), not 008. Running :set nrformats-=octal removes octal recognition and keeps all arithmetic strictly decimal.
How it works
nrformatsis a comma-separated option listing which number formats<C-a>and<C-x>recognize- The default value is
bin,octal,hex(Neovim) oroctal,hex(older Vim) set nrformats-=octalremovesoctalfrom the active list- After this change, integers like
007,0644, or0755are treated as base-10
Example
With default settings, cursor on 007:
007
Press <C-a> → 010 (increments to 8 in octal — unexpected)
After :set nrformats-=octal, cursor on 007:
007
Press <C-a> → 008 (increments to 8 in decimal — expected)
Tips
- Add
set nrformats-=octalto your~/.vimrcorinit.vimto make the change permanent - The confusion is most common with Unix file permission strings like
0644or0755 nrformatscan also containhex(for0xffnumbers) andbin(for0b1010binary literals)- Check the current value with
:set nrformats? - Hexadecimal and binary recognition are usually helpful to keep; only
octaltends to surprise people