2

The behavior of vim's Ctrl A is weird when incrementing numbers <= 0.005.

Due to some personal need, I want to get a set of numbers that increments by 0.005 each time from 0.005, like this:

0.005
0.010
0.015
...

then I thought of vim's macro and Ctrl A.

I entered 0.005 in vim's first line, use y y p Ctrl A to record a macro. But when I moved the cursor to 5 and then pressed Ctrl A continuously, the third time, the number changed directly from 0.007 to 0.010. If I just press 3 times, the output will become:

0.005
0.010
0.013
0.016
...

That means that I cannot complete the task using vim.

After doing this in other ways, I started to be interested in the behavior of vim's Ctrl A.

The text below comes from vim's help manual:

:h CTRL-A:

Add [count] to the number or alphabetic character at or after the cursor.  

and :h count:

An optional number that may precede the command to multiply or iterate the command.
If no number is given, a count of one is used, unless otherwise noted. 

When I tested some other numbers, I found that the behavior becomes weird starting from 0.01. But I still don't know why Ctrl A behaves like this.

Before starting to read the source of vim, does anyone know why vim's Ctrl A behaves like this on decimals?

BTW, My PC environment is Win10, and I use vim_only_x64 downloading from vim's official site.

0

2 Answers 2

5

As @Simson Said, vim thinks it is an octal number and increses 0.07 to 0.10.
You can change this behavior by telling vim not to use octal numbers.

:set nrformats-=octal

Then it increases as expected 0.09 => 0.10

You can see the definition o numbers with :h expr-number

2
  • But it will still consider it only as one integer and increment from 0.99 to 0.100
    – Simson
    Commented Sep 26, 2019 at 8:35
  • Yes you are rigth, in fact it interprets it as to integers seperated by a dot
    – Hans
    Commented Sep 26, 2019 at 8:37
2

Vim does not recognize 0.004 as a decimal fraction, it interpreters it as something.004 The leading 0 of a number encodes an octal number so it increments from 007 to 010

Fun fact it will also recognize hexadecimal numbers 0x09 will be incremented to 0x0a

0

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