首页 > 解决方案 > 为什么当 ~/.vimrc 丢失时 Vim 文件类型选项打开但存在时关闭?

问题描述

我在 Ubuntu 系统上使用 Vim 8.1。这是我发现的三个案例。

情况1

$ ls -l ~/.vimrc
ls: cannot access '/home/lone/.vimrc': No such file or directory
$ vim -u NONE +':filetype'
filetype detection:OFF  plugin:OFF  indent:OFF

案例2

$ ls -l ~/.vimrc
ls: cannot access '/home/lone/.vimrc': No such file or directory
$ vim +':filetype'
filetype detection:ON  plugin:ON  indent:ON

案例3

$ touch ~/.vimrc
$ ls -l ~/.vimrc
-rw-r--r-- 1 lone lone 0 Nov  2 18:41 /home/lone/.vimrc
$ vim +':filetype'
filetype detection:OFF  plugin:OFF  indent:OFF

问题

  1. 为什么filetype选项在存在时~/.vimrc为ON,但在存在时为OFF ~/.vimrc
  2. 在 Vim:help文档的哪里可以找到有关此行为的更多信息?

标签: vimconfigurationdefaultfile-type

解决方案


  1. -u NONE禁用所有 vimrc(包括默认值,更多内容见下文)
  2. 如果没有 vimrc,默认值就会发挥作用(假设你有 vim >7.4.2111 或 >8)
  3. 使用 vimrc,默认值不是来源。

来自:help defaults.vim

Defaults without a .vimrc file ~
                            *defaults.vim*
If Vim is started normally and no user vimrc file is found, the
$VIMRUNTIME/defaults.vim script is loaded.  This will set 'compatible' off,
switch on syntax highlighting and a few more things.  See the script for
details.  NOTE: this is done since Vim 8.0, not in Vim 7.4. (it was added in
patch 7.4.2111 to be exact).

This should work well for new Vim users.  If you create your own .vimrc, it is
recommended to add these lines somewhere near the top: >
    unlet! skip_defaults_vim
    source $VIMRUNTIME/defaults.vim
Then Vim works like before you had a .vimrc. Copying $VIMRUNTIME/vimrc_example
is way to do this.  Alternatively, you can copy defaults.vim to your .vimrc
and modify it (but then you won't get updates when it changes).

If you don't like some of the defaults, you can still source defaults.vim and
revert individual settings.  See the defaults.vim file for hints on how to
revert each item.
                        *skip_defaults_vim*
If you use a system-wide vimrc and don't want defaults.vim to change settings,
set the "skip_defaults_vim" variable.  If this was set and you want to load
defaults.vim from your .vimrc, first unlet skip_defaults_vim, as in the
example above.

推荐阅读