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

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

  • nrformats is a comma-separated option listing which number formats <C-a> and <C-x> recognize
  • The default value is bin,octal,hex (Neovim) or octal,hex (older Vim)
  • set nrformats-=octal removes octal from the active list
  • After this change, integers like 007, 0644, or 0755 are 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-=octal to your ~/.vimrc or init.vim to make the change permanent
  • The confusion is most common with Unix file permission strings like 0644 or 0755
  • nrformats can also contain hex (for 0xff numbers) and bin (for 0b1010 binary literals)
  • Check the current value with :set nrformats?
  • Hexadecimal and binary recognition are usually helpful to keep; only octal tends to surprise people

Next

How do I programmatically set a register's content in Vimscript to pre-load a macro?