When I'm trying to increase numbers with CtrlA it removes the leading and the trailing zeros.

For example:


Results after pressing CtrlA:

1.009500  ->  1.9501
1.09500   ->  1.9501
1.0009500 ->  1.9501

or if cursor in front of the .

1.009500  ->  2.009500
1.09500   ->  2.09500
1.0009500 ->  2.0009500

Is there a way to make it work like this:

1.009500  ->  1.009501
1.09500   ->  1.09501
1.0009500 ->  1.0009501

I have tried this on

vim --version
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Oct 12 2015 04:35:19)
  • cannot reproduce. What Vim version is this? Jul 20 '16 at 13:09
  • 2
    @ChristianBrabandt I can reproduce with a version 7.4.1558 and nrformats=bin,octal,hex
    – statox
    Jul 20 '16 at 13:12
  • @ChristianBrabandt Thank you for your comment. I've edited my question.
    – whitesiroi
    Jul 20 '16 at 13:14
  • 1
    @statox, same here, what is strange is that I have the same result with 009500, but not with 0001, 000100... For the two last one the increment keeps the leading 0
    – nobe4
    Jul 20 '16 at 13:33
  • 1
    It is worth noting that your question cannot actually be solved as stated - you can increment the floating point portion of the number, but not the full floating point number. To see this, try incrementing 1.99 and you will get 1.100 Jul 20 '16 at 17:40

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)
:echo str2nr(000900) 

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');
    if (n++ == maxlen)

Considering the result is un, you can see that the first 0 won't add any value to the decimal number.

  • 4
    So to solve the problem you can simply do set nrformat-=octal
    – statox
    Jul 20 '16 at 13:56
  • 2
    typo.. nrformats or nf
    – Sundeep
    Jul 20 '16 at 14:18
  • @nobe4 OMG, thank you very much for your awesome answer. Thank you guys for your help.
    – whitesiroi
    Jul 20 '16 at 14:35
  • 1
    Wow. +1, this is a very informative and thorough answer.
    – DJMcMayhem
    Jul 20 '16 at 15:05

You found the answer, which is caused by the default Vim setting of 'nrformat'.

However, I'd like to mention another possibility to overcome this problem. This would be to use visual mode, select the last number and press CTRL-A.

Note that this needs a relative recent Vim version, something like 7.4.1000, since visual mode incrementing/decrementing has been added just recently.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.