【问题标题】:VIM defaults.vim not working as expected; some commands only working from home dirVIM defaults.vim 没有按预期工作;一些命令只能在主目录下工作
【发布时间】:2018-05-21 01:34:31
【问题描述】:

系统:运行 Raspbian Stretch 的 Raspberry pi,已全面更新。

这是我的整个 ~/.vimrc 代码。强调底线,这是我用来验证我正在加载正确的 VIMRC 文件的内容。当我以用户身份运行 vim 时,此 VIMRC 中的所有内容都可以正常工作。

" All system-wide defaults are set in $VIMRUNTIME/debian.vim (usually just
" /usr/share/vim/vimcurrent/debian.vim) and sourced by the call to :runtime
" you can find below.  If you wish to change any of those settings, you should
" do it in this file (/etc/vim/vimrc), since debian.vim will be overwritten
" everytime an upgrade of the vim packages is performed.  It is recommended to
" make changes after sourcing debian.vim since it alters the value of the
" 'compatible' option.

" This line should not be removed as it ensures that various options are
" properly set to work with the Vim-related packages available in Debian.
runtime! debian.vim

" Uncomment the next line to make Vim more Vi-compatible
" NOTE: debian.vim sets 'nocompatible'.  Setting 'compatible' changes numerous
" options, so any other options should be set AFTER setting 'compatible'.
"set compatible

" Vim5 and later versions support syntax highlighting. Uncommenting the next
" line enables syntax highlighting by default.
"syntax on

" If using a dark background within the editing area and syntax highlighting
" turn on this option as well
set background=dark

" Uncomment the following to have Vim jump to the last position when
" reopening a file
if has("autocmd")
  au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
endif

" Uncomment the following to have Vim load indentation rules and plugins
" according to the detected filetype.
"if has("autocmd")
"  filetype plugin indent on
"endif

" The following are commented out as they cause vim to behave a lot
" differently from regular Vi. They are highly recommended though.
"set showcmd            " Show (partial) command in status line.
"set showmatch          " Show matching brackets.
"set ignorecase         " Do case insensitive matching
"set smartcase          " Do smart case matching
"set incsearch          " Incremental search
"set autowrite          " Automatically save before commands like :next and :make
"set hidden             " Hide buffers when they are abandoned
"set mouse=a            " Enable mouse usage (all modes)

" Source a global configuration file if available
if filereadable("/etc/vim/vimrc.local")
  source /etc/vim/vimrc.local
endif

syntax on

set mouse-=a

set backspace=indent,eol,start

echo "home vimrc loaded"

但是,当我删除 ~/.vimrc 的副本时,或者(更重要的是,因为这是我遇到的问题),如果我运行 sudo vim,我的全局默认版本 vimrc (/usr/share/vim /vim80/defaults.vim,硬链接到 /etc/vim/vimrc) 不能按预期工作。我特别注意到以下两个问题:

-set mouse-=a 即使该行被注释掉也似乎可以工作。

-set backspace=indent,eol,start 不起作用。

它确实回显了我正在加载全局 vimrc,并且如果我对其进行编辑,回显会相应地进行修改。

这是我的全局 vimrc 文件。

" All system-wide defaults are set in $VIMRUNTIME/debian.vim (usually just
" /usr/share/vim/vimcurrent/debian.vim) and sourced by the call to :runtime
" you can find below.  If you wish to change any of those settings, you should
" do it in this file (/etc/vim/vimrc), since debian.vim will be overwritten
" everytime an upgrade of the vim packages is performed.  It is recommended to
" make changes after sourcing debian.vim since it alters the value of the
" 'compatible' option.

" This line should not be removed as it ensures that various options are
" properly set to work with the Vim-related packages available in Debian.
runtime! debian.vim

" Uncomment the next line to make Vim more Vi-compatible
" NOTE: debian.vim sets 'nocompatible'.  Setting 'compatible' changes numerous
" options, so any other options should be set AFTER setting 'compatible'.
"set compatible

" Vim5 and later versions support syntax highlighting. Uncommenting the next
" line enables syntax highlighting by default.
"syntax on

" If using a dark background within the editing area and syntax highlighting
" turn on this option as well
set background=dark

" Uncomment the following to have Vim jump to the last position when
" reopening a file
if has("autocmd")
  au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
endif

" Uncomment the following to have Vim load indentation rules and plugins
" according to the detected filetype.
"if has("autocmd")
"  filetype plugin indent on
"endif

" The following are commented out as they cause vim to behave a lot
" differently from regular Vi. They are highly recommended though.
"set showcmd            " Show (partial) command in status line.
"set showmatch          " Show matching brackets.
"set ignorecase         " Do case insensitive matching
"set smartcase          " Do smart case matching
"set incsearch          " Incremental search
"set autowrite          " Automatically save before commands like :next and :make
"set hidden             " Hide buffers when they are abandoned
"set mouse=a            " Enable mouse usage (all modes)

" Source a global configuration file if available
if filereadable("/etc/vim/vimrc.local")
  source /etc/vim/vimrc.local
endif

syntax on

set mouse-=a

set backspace=indent,eol,start

echo "global vimrc loaded"

我觉得我一定对 vimrc 默认值的工作方式有误解。任何帮助表示赞赏。

编辑更多信息:

当我创建一个文件 /root/.vimrc 然后运行 ​​sudo vim 时,它确实正确使用了该 vimrc,并且在以 root 运行时一切正常。但是,当我删除本地 vimrc 时,全局文件仍然不起作用。因此,绝对与用户权限无关。除了 vimrc 之外,是否还有第二个 vim 文件可能存在问题?

EDIT2:

:version 的输出

VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Sep 30 2017 18:21:38)
Included patches: 1-197, 322, 377-378, 550, 703, 706-707
Modified by pkg-vim-maintainers@lists.alioth.debian.org
Compiled by pkg-vim-maintainers@lists.alioth.debian.org
Huge version without GUI.  Features included (+) or not (-):
+acl             -clipboard       +dialog_con      +file_in_path    +job             -lua             +mouse_sgr       +path_extra      +rightleft       +tag_old_static  +user_commands   +writebackup
+arabic          +cmdline_compl   +diff            +find_in_path    +jumplist        +menu            -mouse_sysmouse  -perl            -ruby            -tag_any_white   +vertsplit       -X11
+autocmd         +cmdline_hist    +digraphs        +float           +keymap          +mksession       +mouse_urxvt     +persistent_undo +scrollbind      -tcl             +virtualedit     -xfontset
-balloon_eval    +cmdline_info    -dnd             +folding         +lambda          +modify_fname    +mouse_xterm     +postscript      +signs           +termguicolors   +visual          -xim
-browse          +comments        -ebcdic          -footer          +langmap         +mouse           +multi_byte      +printer         +smartindent     +terminfo        +visualextra     -xpm
++builtin_terms  +conceal         +emacs_tags      +fork()          +libcall         -mouseshape      +multi_lang      +profile         +startuptime     +termresponse    +viminfo         -xsmp
+byte_offset     +cryptv          +eval            +gettext         +linebreak       +mouse_dec       -mzscheme        -python          +statusline      +textobjects     +vreplace        -xterm_clipboard
+channel         +cscope          +ex_extra        -hangul_input    +lispindent      +mouse_gpm       +netbeans_intg   -python3         -sun_workshop    +timers          +wildignore      -xterm_save
+cindent         +cursorbind      +extra_search    +iconv           +listcmds        -mouse_jsbterm   +num64           +quickfix        +syntax          +title           +wildmenu
-clientserver    +cursorshape     +farsi           +insert_expand   +localmap        +mouse_netterm   +packages        +reltime         +tag_binary      -toolbar         +windows
   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
       defaults file: "$VIMRUNTIME/defaults.vim"
  fall-back for $VIM: "/usr/share/vim"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H   -Wdate-time  -g -O2 -fdebug-prefix-map=/build/vim-UkjqTY/vim-8.0.0197=. -fstack-protector-strong -Wformat -Werror=format-security -U_FORTIFY_SOURCE -D_FORTIFY_SO
URCE=1
Linking: gcc   -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -o vim        -lm -ltinfo -lnsl  -lselinux -lacl -lattr -lgpm -ldl

:scriptnames 的输出

1: /usr/share/vim/vim80/defaults.vim
  2: /usr/share/vim/vim80/syntax/syntax.vim
  3: /usr/share/vim/vim80/syntax/synload.vim
  4: /usr/share/vim/vim80/syntax/syncolor.vim
  5: /usr/share/vim/vim80/filetype.vim
  6: /usr/share/vim/vim80/plugin/getscriptPlugin.vim
  7: /usr/share/vim/vim80/plugin/gzip.vim
  8: /usr/share/vim/vim80/plugin/logiPat.vim
  9: /usr/share/vim/vim80/plugin/manpager.vim
 10: /usr/share/vim/vim80/plugin/matchparen.vim
 11: /usr/share/vim/vim80/plugin/netrwPlugin.vim
 12: /usr/share/vim/vim80/plugin/rrhelper.vim
 13: /usr/share/vim/vim80/plugin/spellfile.vim
 14: /usr/share/vim/vim80/plugin/tarPlugin.vim
 15: /usr/share/vim/vim80/plugin/tohtml.vim
 16: /usr/share/vim/vim80/plugin/vimballPlugin.vim
 17: /usr/share/vim/vim80/plugin/zipPlugin.vim
 18: /usr/share/vim/vim80/syntax/vim.vim

【问题讨论】:

  • :version 的完整输出是什么? :scriptnames 的完整输出是多少?你知道$ sudo -e吗?
  • romainl,我已经编辑了带有输出的原始帖子。我相信 sudo -e 会完成与 sudoedit 类似的事情,它会在哪里维护我的设置?如果是这样,我知道该选项并且可能会长期使用它,只是希望了解为什么 defaults.vim 无法按预期工作。

标签: vim raspbian


【解决方案1】:

$VIMRUNTIME/defaults.vim(扩展为 /usr/share/vim/vim80/runtime/defaults.vim)不是你的全局 vimrc(除非 raspbian 设置不同)。通常在 debian 系统上,/etc/vim/vimrc 硬链接到 /usr/share/vim/vimrc,这是默认的 system vimrc 文件。无论您是谁,此文件始终都会获得来源(请参阅:h system-vimrc)。 defaults.vim 是一个行为怪异的特殊文件;来自:h defaults

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.

所以defaults.vim 将用于每个没有自己的vimrc 的用户,它设置mouse=a 等等。这就是为什么创建 /root/.vimrc 可以让事情按预期工作的原因。

我建议不要更改 /usr/share/vim/vimrc 或 defaults.vim,因为当 vim 更新时,这两个文件都会被覆盖。如果您想以 root 身份使用 vimrc,则可以使用 sudoedit 之类的东西而不是登录到 root 用户,或者使用 vim -u /path/to/my/vimrc 之类的东西从 root 用户启动 vim。

【讨论】:

  • 感谢 flemingfleming 的详细回复。 /usr/share/vim/ 里面没有 vimrc 文件;只有 3 个文件夹、插件、注册表和 vim80,它们中似乎也没有一个 vimrc。它肯定是从 defaults.vim 加载的,因为当我编辑 echo 语句时,当我从没有 ~/.vimrc 的用户重新加载 vim 时,它会反映出来。我知道 sudoedit,并且可能会将其用于我的长期解决方案,但希望能够了解为什么 defaults.vim 无法按预期工作。
  • 预计defaults.vim将在用户没有vimrc时被加载,这就是它的用途。我猜 rasbian 的设置与 debain 有点不同,在本地我希望/etc/vim/vimrc 可以采购。您可以查看$VIM/vimrc 指向的位置,并查看:h .vimrc,它清楚地列出了完整的初始化顺序,看看是否还有其他需要注意的地方。
猜你喜欢
  • 1970-01-01
  • 2012-03-14
  • 2023-03-17
  • 1970-01-01
  • 2014-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-24
相关资源
最近更新 更多