忽略vimrc中的"未知选项"错误

spi*_*ech 17 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
Run Code Online (Sandbox Code Playgroud)

我怎样才能让Vim忽略这些错误,并且每当我打开文件时都不会提示我按Enter键?

ech*_*son 19

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

例如:

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
Run Code Online (Sandbox Code Playgroud)


sid*_*yll 10

将新选项包装在:

if version >= 703
  set rnu ...
endif
Run Code Online (Sandbox Code Playgroud)

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.
Run Code Online (Sandbox Code Playgroud)

  • 测试特定功能比测试更好,因为可以在没有您要查找的功能的情况下编译特定版本. (2认同)
  • 测试功能要好得多.Fedora 21的默认vim是7.4,但编译时没有折叠,这是在6.0中添加的. (2认同)

kbu*_*ien 7

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

$ vi
Error detected while processing /home/username/.vimrc:
line    9:
Unknown option: indentexpr=
Run Code Online (Sandbox Code Playgroud)

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

if exists("&indentexpr")
  :set indentexpr=
endif
Run Code Online (Sandbox Code Playgroud)


Boh*_*ohr 5

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