【问题标题】:Ignore "Unknown option" errors in vimrc忽略 vimrc 中的“未知选项”错误
【发布时间】:2014-04-11 22:25:31
【问题描述】:

我在同时安装了 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 键?

【问题讨论】:

    标签: vim


    【解决方案1】:

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

    例如:

    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
    

    【讨论】:

      【解决方案2】:

      将新选项包装在:

      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.
      

      【讨论】:

      • 那行得通。绝对要小心设置版本号 - 我花了一些时间仔细阅读,发现它只是“版本 * 100”
      • 是的,这可能很棘手,但你会习惯它。我宁愿用字符串连接的方式来描述它,而不是做数学。请注意,这在许多其他地方使用。例如,Perl 使用与use 类似的语法。您必须 use 5.010; 才能使用 perl 5.10 的功能,而不是 5.1。这就是他们不使用十进制意义的原因,否则你会产生一些歧义。
      • 测试特定功能比测试版本更好,因为可以编译特定版本而没有您正在寻找的功能。
      • 更好地测试功能。 Fedora 21 的默认 vim 是 7.4,但编译时没有折叠,这是在 6.0 中添加的。
      • 绝对应该检查所需的功能,而不是假设特定的版本号就足够了。
      【解决方案3】:

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

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

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

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

      【讨论】:

      • 这不是摆弄版本号。
      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

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

        help v:version

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

        703 对应于 Vim 7.3(这不是很直观...)

        【讨论】:

          【解决方案6】:

          我想说这个问题没有答案。考虑在计算机 A 上创建的具有更高 vim 版本的 Session.vim。在源代码控制中,当另一台计算机 B 尝试打开 Session.vim 时,会触发错误。必须为应该是自动化的过程手动包装版本号是没有意义的。有了这种行为,新版本必须在保存会话时自动将新命令包装在版本号中——这是 7.3 不做的。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-10-13
            • 1970-01-01
            • 1970-01-01
            • 2018-03-18
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多