vim配置文件结构

vim配置文件结构

在写本文之前,我已经写了好几篇关于vim的文章,按时间先后顺序分别为:

突然发现我脑子里一直没有一个清晰的vim配置文件结构,本文就来梳理一下“vim配置文件结构”。

vim到底加载了哪些配置?

大多数vim使用者,应该是知道我们可以通过在~/.vimrc文件中添加一些配置项来配置vim的吧?就类似~/.bashrc~/.zshrc,其实上这只是我们最常用的配置方式,只能用来设置一些选项,如果是插件,比如一些语法高亮插件等等,它的内容比较多,就无法直接放到.vimrc中了,所以就需要放在其它地方。

我们先来看一下vim启动时究竟加载了哪些配置,用vim打开任意一个文件,或者直接打开vim也行,然后在底行模式输入以下命令,按回车

:scriptnames

以下是我Debian Linux服务器上的vim输出的结果,可以看到除了~/.vimrc,其它都是vim自带的配置

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

以下是我的macOS上的vim输出的结果,可以看到除了~/.vimrc,还加载了非常多的脚本,这是因为我安装了一些插件(插件的本质也是脚本)

1: /usr/local/Cellar/macvim/9.0.1276/MacVim.app/Contents/Resources/vim/vimrc
2: ~/.vimrc
3: ~/.vim/autoload/pathogen.vim
4: /usr/local/Cellar/macvim/9.0.1276/MacVim.app/Contents/Resources/vim/runtime/syntax/syntax.vim
5: /usr/local/Cellar/macvim/9.0.1276/MacVim.app/Contents/Resources/vim/runtime/syntax/synload.vim
6: /usr/local/Cellar/macvim/9.0.1276/MacVim.app/Contents/Resources/vim/runtime/syntax/syncolor.vim
7: ~/.vim/filetype.vim
8: /usr/local/Cellar/macvim/9.0.1276/MacVim.app/Contents/Resources/vim/runtime/filetype.vim
9: /usr/local/Cellar/macvim/9.0.1276/MacVim.app/Contents/Resources/vim/runtime/scripts.vim
10: /usr/local/Cellar/macvim/9.0.1276/MacVim.app/Contents/Resources/vim/runtime/autoload/dist/script.vim
11: ~/.vim/bundle/nerdtree/plugin/NERD_tree.vim
12: ~/.vim/bundle/nerdtree/autoload/nerdtree.vim
13: ~/.vim/bundle/nerdtree/lib/nerdtree/path.vim
14: ~/.vim/bundle/nerdtree/lib/nerdtree/menu_controller.vim
15: ~/.vim/bundle/nerdtree/lib/nerdtree/menu_item.vim
16: ~/.vim/bundle/nerdtree/lib/nerdtree/key_map.vim
17: ~/.vim/bundle/nerdtree/lib/nerdtree/bookmark.vim
18: ~/.vim/bundle/nerdtree/lib/nerdtree/tree_file_node.vim
19: ~/.vim/bundle/nerdtree/lib/nerdtree/tree_dir_node.vim
20: ~/.vim/bundle/nerdtree/lib/nerdtree/opener.vim
21: ~/.vim/bundle/nerdtree/lib/nerdtree/creator.vim
22: ~/.vim/bundle/nerdtree/lib/nerdtree/flag_set.vim
23: ~/.vim/bundle/nerdtree/lib/nerdtree/nerdtree.vim
24: ~/.vim/bundle/nerdtree/lib/nerdtree/ui.vim
25: ~/.vim/bundle/nerdtree/lib/nerdtree/event.vim
26: ~/.vim/bundle/nerdtree/lib/nerdtree/notifier.vim
27: ~/.vim/bundle/nerdtree/autoload/nerdtree/ui_glue.vim
28: ~/.vim/bundle/nerdtree/nerdtree_plugin/exec_menuitem.vim
29: ~/.vim/bundle/nerdtree/nerdtree_plugin/fs_menu.vim
30: ~/.vim/bundle/vim-mucomplete/plugin/mucomplete.vim
31: /usr/local/Cellar/macvim/9.0.1276/MacVim.app/Contents/Resources/vim/runtime/plugin/getscriptPlugin.vim
32: /usr/local/Cellar/macvim/9.0.1276/MacVim.app/Contents/Resources/vim/runtime/plugin/gzip.vim
33: /usr/local/Cellar/macvim/9.0.1276/MacVim.app/Contents/Resources/vim/runtime/plugin/logiPat.vim
34: /usr/local/Cellar/macvim/9.0.1276/MacVim.app/Contents/Resources/vim/runtime/plugin/manpager.vim
35: /usr/local/Cellar/macvim/9.0.1276/MacVim.app/Contents/Resources/vim/runtime/plugin/matchparen.vim
36: /usr/local/Cellar/macvim/9.0.1276/MacVim.app/Contents/Resources/vim/runtime/plugin/netrwPlugin.vim
37: /usr/local/Cellar/macvim/9.0.1276/MacVim.app/Contents/Resources/vim/runtime/plugin/rrhelper.vim
38: /usr/local/Cellar/macvim/9.0.1276/MacVim.app/Contents/Resources/vim/runtime/plugin/spellfile.vim
39: /usr/local/Cellar/macvim/9.0.1276/MacVim.app/Contents/Resources/vim/runtime/plugin/tarPlugin.vim
40: /usr/local/Cellar/macvim/9.0.1276/MacVim.app/Contents/Resources/vim/runtime/plugin/tohtml.vim
41: /usr/local/Cellar/macvim/9.0.1276/MacVim.app/Contents/Resources/vim/runtime/plugin/vimballPlugin.vim
42: /usr/local/Cellar/macvim/9.0.1276/MacVim.app/Contents/Resources/vim/runtime/plugin/zipPlugin.vim
43: /usr/local/powerline/powerline/bindings/vim/plugin/powerline.vim

vim的runtimepath

runtimepath是vim的运行时路径,它用于指定vim需要从哪些目录中加载.vim结尾的vim脚本,可简写为rtp,在vim底行模式运行:set runtimepath:set rtp,会输出该变量的值。

以下是我Debian Linux服务器的runtimepath

runtimepath=~/.vim,/usr/local/vim/share/vim/vimfiles,/usr/local/vim/share/vim/vim82,/usr/local/vim/share/vim/vimfiles/after,~/.vim/after,/usr/local/powerline/powerline/bindings/vim

以下是我macOS上的runtimepath

runtimepath=~/.vim,~/.vim/bundle/nerdtree,~/.vim/bundle/vim-mucomplete,/usr/local/Cellar/macvim/9.0.1276/MacVim.app/Contents/Resources/vim/vimfiles,/usr/local/Cellar/macvim/9.0.12
76/MacVim.app/Contents/Resources/vim/runtime,/usr/local/Cellar/macvim/9.0.1276/MacVim.app/Contents/Resources/vim/vimfiles/after,~/.vim/after,/usr/local/powerline/powerline/bindings/vim

根据以上内容可知,vim会从rtp(或叫runtimepath)中指定的文件夹中去搜索.vim后缀的文件,然后去执行它(非.vim结尾文件不会被识别)。

至于加载vim配置的路径,有默认的,有自定义的,默认的,不同系统,不同安装方法可能所在的地方都不一样,但这不重要,重要的是自定义的配置放在哪儿?答案是放在~/.vim/目录下。

另外如果你有使用过一些插件,这些插件的使用方法可能要求你在~/.vimrc中添加set rtp+=/path/to/插件,就是为了让vim从rtp中读取它,此时插件的位置就不是必须要放在~/.vim/下了,因为你已经手动把它添加到rtp中了。

~/.vim/目录结构

由以上可知,我们自定义vim配置,小配置放在~/.vimrc中,大配置(如插件)可放在~/.vim/中。

vim-fishnginx配置文件的vim支持文件可知,一般~/.vim/目录下存在以下几个目录

  • ftdetect:ft是filetype,detect是检测,所以该文件夹用户放置文件类型检测相关的脚本;
  • ftplugin:ft是filetype,后面是plugin,代表插件,别看它前三个字母是ftp就以为跟ftp有关,其实跟ftp并没有任何关系,纯属巧合;
  • indent:缩进的意思,放置缩进的脚本;
  • syntax:语法的意思,用户放置语法高亮的脚本,这个通常是我们最需要的;

事实上这些文件夹只是为了分类,方便管理,其实你把这些文件夹的文件互换,都丝毫不会影响这些文件夹中的脚本被正确加载,因为无论它们在哪个文件夹,都是一样加载的,只是我们人为区分为了方便管理罢了。但要注意,虽然这些文件夹中的文件可以互换,但这些文件夹的名字却是固定不能变的,比如你syntax改成syntax2,语法高亮立马就没了。

另外如果你安装了插件管理器,如pathogen,会要求在~/.vim/下创建autoload文件夹,该文件夹用于放置插件管理器文件,它就不能随意移动了。


其实还有另一种形式,比如vim-fish,它里面有很多文件夹

UltiSnips
autoload
bin
compiler
ftdetect
ftplugin
indent
syntax
syntax_checkers/fish

你既可以把它里面的ftdetect、ftplugin、indent、syntax四个文件夹内的文件分别放到~/.vim/下对应的四个文件夹(没有就创建),也可以把整个vim-fish项目clone下来直接放在~/.vim/下,这样它就相当于一个插件,当然这样它是不会被直接加载的,你需要在~/.vimrc中添加set rtp+=~/.vim/vim-fish

当然如果你安装了pathogen插件管理器,你可以直接把整个vim-fish放在bundle文件夹下,然后在.vimrc中不需要设置rtp,插件管理器会自动加载它。

打赏
订阅评论
提醒
guest

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

0 评论
内联反馈
查看所有评论
0
希望看到您的想法,请您发表评论x

扫码在手机查看
iPhone请用自带相机扫
安卓用UC/QQ浏览器扫

vim配置文件结构