Vim E518错误:未知选项

19

我在Unix系统上有一个文本文件。以下的文本文件内容导致了问题:

good: ok line
vi: bad line
ok: ok line

所以,如果我运行:vim test.txt,我会得到以下错误:

"test.txt" 3L, 39C
Error detected while processing modelines:
line    2:
E518: Unknown option: bad
Press ENTER or type command to continue

如果我删除我的~/.vimrc,错误就消失了。但是奇怪的是,即使是空的~/.vimrc文件,错误仍然出现。

我知道这是因为这一行以vi:开头而产生的错误,但我不明白为什么或如何解决它。

3个回答

21

vi: bad line 行以一种 Vim 认为是模型行的格式呈现,正如错误信息中所提到的。模型行允许在文件内设置选项。

当没有 ~/.vimrc 文件时不会触发该行的原因是 Vim 要求默认情况下启用模型行需要设置 'nocompatible',因为这是 Vim 特有的功能。然而,存在 ~/.vimrc 文件足以使 Vim 从 vi 兼容模式切换到无兼容模式,这也将导致 'modeline' 选项被设置。

对于以后的参考,您可以通过 :help topic<Tab> 在 Vim 中轻松找到帮助主题。在这种情况下,:help modeline<Tab> 将为您提供一些主题,解释此功能及如何控制它。


谢谢您。我会将此答案标记为已接受,因为它解释了正在发生的事情以及为什么(vimrc 的存在)。 - Danosaure
因为vim将最后的n行视为modelines(即vim设置),所以最好保持代码的最后n行为空行或实际的vim modelines,以避免出现错误。 - shahjapan

16

你可以通过以下方式关闭模型线处理:

你可以通过以下方式关闭模型线处理:

:set nomodeline

更多信息请参见:help modeline.


1
:help auto-setting下,您会找到以下段落:
3. If you start editing a new file, and the 'modeline' option is on, a
   number of lines at the beginning and end of the file are checked for
   modelines.  This is explained here. 

There are two forms of modelines.  The first form:
    [text]{white}{vi:|vim:|ex:}[white]{options}

[text]      any text or empty
{white}     at least one blank character (<Space> or <Tab>)
{vi:|vim:|ex:}  the string "vi:", "vim:" or "ex:"
[white]     optional white space
{options}   a list of option settings, separated with white space or ':',
        where each part between ':' is the argument for a ":set"
        command (can be empty)

Example:
   vi:noai:sw=3 ts=6 ~

The second form (this is compatible with some versions of Vi):

    [text]{white}{vi:|vim:|ex:}[white]se[t] {options}:[text]

[text]      any text or empty
{white}     at least one blank character (<Space> or <Tab>)
{vi:|vim:|ex:}  the string "vi:", "vim:" or "ex:"
[white]     optional white space
se[t]       the string "set " or "se " (note the space)
{options}   a list of options, separated with white space, which is the
        argument for a ":set" command
:       a colon
[text]      any text or empty

Example:
   /* vim: set ai tw=75: */ ~

The white space before {vi:|vim:|ex:} is required.  This minimizes the chance
that a normal word like "lex:" is caught.  There is one exception: "vi:" and
"vim:" can also be at the start of the line (for compatibility with version
3.0).  Using "ex:" at the start of the line will be ignored (this could be
short for "example:").

所以,很可能在您的~/.vimrc文件中有一个set nomodeline

读取vi: bad line的行尝试设置选项badline,这些选项无法设置,因此出现错误。

编辑:正如jamessan'S答案(+1)所指出的那样,通过存在~/.vimrc文件,通过设置nocompatible来设置modeline选项。


非常感谢您的解释。奇怪的是,我的代码只是1000多行中的第696行(它是一个大的/etc/group文件)。但这解决了我的问题。 - Danosaure

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接