我在同时安装了 Vim 7.2 和 7.3 的机器之间使用相同的 .vimrc。每次打开文件时,使用 Vim 7.2 的机器都会抱怨我的 7.3 特定选项:
Error detected while processing /home/spiffytech/.vimrc:
line 72:
E518: Unknown option: rnu
line 73:
E518: Unknown option: undofile
line 74:
E518: Unknown option: undodir=/tmp
line 75:
E518: Unknown option: cryptmethod=blowfish
Press ENTER or type command to continue
如何让 Vim 忽略这些错误,并且在打开文件时不提示我按回车键?
可能值得对实际支持的功能而不是版本进行更细粒度的检查。
例如:
if has('persistent_undo')
set undofile
set undodir=/tmp
endif
" Some options can only be checked with exists('+option'); I'm not sure why
if exists('+relativenumber')
set rnu
endif
if has('cryptv')
set cryptmethod=blowfish
end
新选项包装在:
if version >= 703
set rnu ...
endif
有关要使用的版本号的详细信息,请查看v:version
帮助:
*v:version* *version-variable*
v:version Version number of Vim: Major version number times 100 plus
minor version number. Version 5.0 is 500. Version 5.1 (5.01)
is 501. Read-only. "version" also works, for backwards
compatibility.
Use |has()| to check if a certain patch was included, e.g.: >
if has("patch123")
< Note that patch numbers are specific to the version, thus both
version 5.0 and 5.1 may have a patch 123, but these are
completely different.
有时某个选项是合法的,但在当前环境中不可用。 例如:
$ vi
Error detected while processing /home/username/.vimrc:
line 9:
Unknown option: indentexpr=
要测试某个选项是否存在,并在选项不可用时避免错误,请执行以下操作:
if exists("&indentexpr")
:set indentexpr=
endif
您可以忽略任何带有 silent! ...
的错误,例如silent! set undofile
在 .vimrc 中,您可以针对正在执行的 Vim 版本进行测试。
请参阅help v:version
if v:version >= 703
"do something
set rnu
set undofile
...
endif
703 对应于 Vim 7.3(这不是很直观......
说这个问题没有得到回答。 考虑在计算机 A 上创建的 Session.vim,该计算机具有较新的 vim 版本。 在源代码管理中,当另一台计算机 B 尝试打开 Session.vim 时,将触发错误。 必须手动包装本应是自动化过程的版本号是没有意义的。通过这种行为,新版本必须在保存会话时自动将新命令包装在版本号中 - 这是 7.3 不会做的事情。