当递增数字 <= 0.005时, vim 的行为Ctrl A很奇怪。
由于一些个人需要,我想得到一组从0.005开始每次递增0.005的数字,如下所示:
0.005
0.010
0.015
...
Run Code Online (Sandbox Code Playgroud)
然后我想到了vim的宏和Ctrl A.
我在vim的第一行输入了0.005,用于y y p Ctrl A录制宏。但当我把光标移到5上,然后Ctrl A连续按第三次时,数字直接从0.007变成了0.010。如果我只按 3 次,输出将变成:
0.005
0.010
0.013
0.016
...
Run Code Online (Sandbox Code Playgroud)
这意味着我无法使用 vim 完成任务。
在以其他方式这样做之后,我开始对 vim 的行为感兴趣Ctrl A。
以下文字来自vim的帮助手册:
:h CTRL-A:
Add [count] to the number or alphabetic character at or after the cursor.
Run Code Online (Sandbox Code Playgroud)
和: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.
Run Code Online (Sandbox Code Playgroud)
当我测试其他一些数字时,我发现从 0.01 开始行为变得很奇怪。但我仍然不知道为什么Ctrl A会有这样的行为。
在开始阅读 vim 的源代码之前,有谁知道为什么 vimCtrl A在小数上的表现如此?
顺便说一句,我的电脑环境是Win10,我使用从vim官方网站下载的vim_only_x64。
正如@Simson所说,vim认为它是一个八进制数并增加0.07到0.10。
您可以通过告诉 vim 不要使用八进制数字来更改此行为。
:set nrformats-=octal
Run Code Online (Sandbox Code Playgroud)
然后它按预期增加 0.09 => 0.10
您可以使用以下命令查看数字的定义:h expr-number