Thanks to @statox's suggestion, you can solve your issue with:
:set nrformats-=octal
Firstly, Vim does not increment decimal number, it will try to increment 1
and 009500
separately. So the question is why incrementing 009500
removes the leading 0
.
As suggested by @sp asic, I think vim is treating this number as an octal number.
Looking at the source code of Vim, a conversion between the string and the actual number is done, using the str2nr()
function.
You can see the definition of numbers in :h expr-number
:
hex-number octal-number
Decimal, Hexadecimal (starting with 0x or 0X), or Octal (starting with 0).
009500
is a number starting with a 0
, but containing a 9
, it's not an octal number, and should not have a leading 0
.
You can see the same result calling the str2nr()
function yourself:
:echo str2nr(000100)
64
:echo str2nr(000900)
900
00100
is a valid octal number, incrementing it will just add 1
to the result.
00900
is an invalid decimal number, incrementing it will fix it (removing the leading 0
) and incrementing it.
Note: This is what I've understood of the little dig I've done in the Vim code, as I didn't understood everything I've read I may be wrong.
I believe the code responsible for the decimal conversion is the following (source):
while (VIM_ISDIGIT(*ptr))
{
un = 10 * un + (uvarnumber_T)(*ptr - '0');
++ptr;
if (n++ == maxlen)
break;
}
Considering the result is un
, you can see that the first 0
won't add any value to the decimal number.
nrformats=bin,octal,hex
009500
, but not with0001
,000100
... For the two last one the increment keeps the leading0