忽略vimrc中的“未知选项”错误

时间:2012-06-14 15:01:30

标签: vim

我在同时安装了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忽略这些错误,并且每当我打开文件时都不会提示我按Enter键?

6 个答案:

答案 0 :(得分:19)

对于实际支持的功能而不是版本,可能值得进行更细粒度的检查。

E.g:

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

答案 1 :(得分:10)

将新选项包装在:

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.

答案 2 :(得分:7)

有时候选项是合法的,但在当前环境中不可用。例如:

$ vi
Error detected while processing /home/username/.vimrc:
line    9:
Unknown option: indentexpr=

测试选项是否存在,如果不可用则避免错误:

if exists("&indentexpr")
  :set indentexpr=
endif

答案 3 :(得分:4)

您可以忽略silent! ...的任何错误,例如silent! set undofile

答案 4 :(得分:3)

在.vimrc中,您可以测试正在执行的Vim版本。

请参阅help v:version

if v:version >= 703
    "do something
    set rnu
    set undofile
    ...
endif

703 对应于Vim 7.3(实际上并不直观......)

答案 5 :(得分:0)

我会说这个问题没有回答。考虑在计算机A上创建的Session.vim,它具有更高版本的vim。在源控件中,当另一台计算机B尝试打开Session.vim时,会触发错误。没有必要手动包装应该是自动化过程的版本号。使用此行为时,新版本必须在保存会话时自动将新命令包装在版本号中 - 这是7.3不能执行的操作。

相关问题