这是一篇学习日志,更多的是记录,帮助自己记忆。因为自己也是小白,所以文章会很低端很琐碎。
我们先来捋一下思路:既然是安装vim的插件!那么首先要安装vim;然后是安装插件(这里有两个问题:一个是去哪里下载插件;二个是怎么下载插件);最后是让vim启动时加载插件。
下载什么的都无所谓!重要的是理解vim是如何加载插件的。那么......去哪里了解vim的初始化过程呢?打开vim,在命令模式下输入以下命令便能看到vim的初始化说明文档。
:h initialization
那我们就一起来了解一下vim的初始化过程吧!可能稍微会有点长......但是,没关系,我会为你翻译的!
4. Initialization initialization startup
This section is about the non-GUI version of Vim. See gui-fork for
additional initialization when starting the GUI.
At startup, Vim checks environment variables and files and sets values
accordingly. Vim proceeds in this order:
1. Set the 'shell' and 'term' option SHELL COMSPEC TERM
The environment variable SHELL, if it exists, is used to set the
'shell' option. On MS-DOS and Win32, the COMSPEC variable is used
if SHELL is not set.
The environment variable TERM, if it exists, is used to set the 'term'
option. However, 'term' will change later when starting the GUI (step
8 below).
2. Process the arguments
The options and file names from the command that start Vim are
inspected. Buffers are created for all files (but not loaded yet).
The -V argument can be used to display or log what happens next,
useful for debugging the initializations.
3. Execute Ex commands, from environment variables and/or files
An environment variable is read as one Ex command line, where multiple
commands must be separated with '|' or "<NL>".
vimrc exrc
A file that contains initialization commands is called a "vimrc" file.
Each line in a vimrc file is executed as an Ex command line. It is
sometimes also referred to as "exrc" file. They are the same type of
file, but "exrc" is what Vi always used, "vimrc" is a Vim specific
name. Also see vimrc-intro.
Places for your personal initializations:
Unix $HOME/.vimrc or $HOME/.vim/vimrc
OS/2 $HOME/.vimrc, $HOME/vimfiles/vimrc
or $VIM/.vimrc (or _vimrc)
MS-Windows $HOME/_vimrc, $HOME/vimfiles/vimrc
or $VIM/_vimrc
Amiga s:.vimrc, home:.vimrc, home:vimfiles:vimrc
or $VIM/.vimrc
The files are searched in the order specified above and only the first
one that is found is read.
RECOMMENDATION: Put all your Vim configuration stuff in the
$HOME/.vim/ directory ($HOME/vimfiles/ for MS-Windows). That makes it
easy to copy it to another system.
If Vim was started with "-u filename", the file "filename" is used.
All following initializations until 4. are skipped. $MYVIMRC is not
set.
"vim -u NORC" can be used to skip these initializations without
reading a file. "vim -u NONE" also skips loading plugins. -u
If Vim was started in Ex mode with the "-s" argument, all following
initializations until 4. are skipped. Only the "-u" option is
interpreted.
evim.vim
a. If vim was started as evim or eview or with the -y argument, the
script $VIMRUNTIME/evim.vim will be loaded.
b. For Unix, MS-DOS, MS-Windows, OS/2, VMS, Macintosh, RISC-OS and Amiga
the system vimrc file is read for initializations. The path of this
file is shown with the ":version" command. Mostly it's "$VIM/vimrc".
Note that this file is ALWAYS read in 'compatible' mode, since the
automatic resetting of 'compatible' is only done later. Add a ":set
nocp" command if you like.
For the Macintosh the $VIMRUNTIME/macmap.vim is read.
VIMINIT .vimrc _vimrc EXINIT .exrc _exrc $MYVIMRC
c. Five places are searched for initializations. The first that exists
is used, the others are ignored. The $MYVIMRC environment variable is
set to the file that was first found, unless $MYVIMRC was already set
and when using VIMINIT.
I The environment variable VIMINIT (see also compatible-default) (*)
The value of $VIMINIT is used as an Ex command line.
II The user vimrc file(s):
"$HOME/.vimrc" (for Unix and OS/2) (*)
"$HOME/.vim/vimrc" (for Unix and OS/2) (*)
"s:.vimrc" (for Amiga) (*)
"home:.vimrc" (for Amiga) (*)
"home:vimfiles:vimrc" (for Amiga) (*)
"$VIM/.vimrc" (for OS/2 and Amiga) (*)
"$HOME/_vimrc" (for MS-DOS and Win32) (*)
"$HOME/vimfiles/vimrc" (for MS-DOS and Win32) (*)
"$VIM/_vimrc" (for MS-DOS and Win32) (*)
Note: For Unix, OS/2 and Amiga, when ".vimrc" does not exist,
"_vimrc" is also tried, in case an MS-DOS compatible file
system is used. For MS-DOS and Win32 ".vimrc" is checked
after "_vimrc", in case long file names are used.
Note: For MS-DOS and Win32, "$HOME" is checked first. If no
"_vimrc" or ".vimrc" is found there, "$VIM" is tried.
See $VIM for when $VIM is not set.
III The environment variable EXINIT.
The value of $EXINIT is used as an Ex command line.
IV The user exrc file(s). Same as for the user vimrc file, but with
"vimrc" replaced by "exrc". But only one of ".exrc" and "_exrc" is
used, depending on the system. And without the (*)!
V The default vimrc file, $VIMRUNTIME/defaults.vim. This sets up
options values and has "syntax on" and "filetype on" commands,
which is what most new users will want. See defaults.vim.
d. If the 'exrc' option is on (which is NOT the default), the current
directory is searched for three files. The first that exists is used,
the others are ignored.
- The file ".vimrc" (for Unix, Amiga and OS/2) (*)
"_vimrc" (for MS-DOS and Win32) (*)
- The file "_vimrc" (for Unix, Amiga and OS/2) (*)
".vimrc" (for MS-DOS and Win32) (*)
- The file ".exrc" (for Unix, Amiga and OS/2)
"_exrc" (for MS-DOS and Win32)
(*) Using this file or environment variable will cause 'compatible' to be
off by default. See compatible-default.
Note: When using the mzscheme interface, it is initialized after loading
the vimrc file. Changing 'mzschemedll' later has no effect.
4. Load the plugin scripts. load-plugins
This does the same as the command:
:runtime! plugin/**/*.vim
The result is that all directories in the 'runtimepath' option will be
searched for the "plugin" sub-directory and all files ending in ".vim"
will be sourced (in alphabetical order per directory), also in
subdirectories.
However, directories in 'runtimepath' ending in "after" are skipped
here and only loaded after packages, see below.
Loading plugins won't be done when:
- The 'loadplugins' option was reset in a vimrc file.
- The --noplugin command line argument is used.
- The --clean command line argument is used.
- The "-u NONE" command line argument is used -u.
- When Vim was compiled without the +eval feature.
Note that using "-c 'set noloadplugins'" doesn't work, because the
commands from the command line have not been executed yet. You can
use "--cmd 'set noloadplugins'" or "--cmd 'set loadplugins'" --cmd.
Packages are loaded. These are plugins, as above, but found in the
"start" directory of each entry in 'packpath'. Every plugin directory
found is added in 'runtimepath' and then the plugins are sourced. See
packages.
The plugins scripts are loaded, as above, but now only the directories
ending in "after" are used. Note that 'runtimepath' will have changed
if packages have been found, but that should not add a directory
ending in "after".
5. Set 'shellpipe' and 'shellredir'
The 'shellpipe' and 'shellredir' options are set according to the
value of the 'shell' option, unless they have been set before.
This means that Vim will figure out the values of 'shellpipe' and
'shellredir' for you, unless you have set them yourself.
6. Set 'updatecount' to zero, if "-n" command argument used
7. Set binary options
If the "-b" flag was given to Vim, the options for binary editing will
be set now. See -b.
8. Perform GUI initializations
Only when starting "gvim", the GUI initializations will be done. See
gui-init.
9. Read the viminfo file
If the 'viminfo' option is not empty, the viminfo file is read. See
viminfo-file.
10. Read the quickfix file
If the "-q" flag was given to Vim, the quickfix file is read. If this
fails, Vim exits.
11. Open all windows
When the -o flag was given, windows will be opened (but not
displayed yet).
When the -p flag was given, tab pages will be created (but not
displayed yet).
When switching screens, it happens now. Redrawing starts.
If the "-q" flag was given to Vim, the first error is jumped to.
Buffers for all windows will be loaded.
12. Execute startup commands
If a "-t" flag was given to Vim, the tag is jumped to.
The commands given with the -c and +cmd arguments are executed.
If the 'insertmode' option is set, Insert mode is entered.
The starting flag is reset, has("vim_starting") will now return zero.
The v:vim_did_enter variable is set to 1.
The VimEnter autocommands are executed.
The $MYVIMRC or $MYGVIMRC file will be set to the first found vimrc and/or
gvimrc file.
Some hints on using initializations
Standard setup:
Create a vimrc file to set the default settings and mappings for all your edit
sessions. Put it in a place so that it will be found by 3b:
~/.vimrc (Unix and OS/2)
s:.vimrc (Amiga)
$VIM\_vimrc (MS-DOS and Win32)
Note that creating a vimrc file will cause the 'compatible' option to be off
by default. See compatible-default.
Local setup:
Put all commands that you need for editing a specific directory only into a
vimrc file and place it in that directory under the name ".vimrc" ("_vimrc"
for MS-DOS and Win32). NOTE: To make Vim look for these special files you
have to turn on the option 'exrc'. See trojan-horse too.
System setup:
This only applies if you are managing a Unix system with several users and
want to set the defaults for all users. Create a vimrc file with commands
for default settings and mappings and put it in the place that is given with
the ":version" command.
Saving the current state of Vim to a file
Whenever you have changed values of options or when you have created a
mapping, then you may want to save them in a vimrc file for later use. See
save-settings about saving the current state of settings to a file.
Avoiding setup problems for Vi users
Vi uses the variable EXINIT and the file "~/.exrc". So if you do not want to
interfere with Vi, then use the variable VIMINIT and the file "vimrc" instead.
Amiga environment variables
On the Amiga, two types of environment variables exist. The ones set with the
DOS 1.3 (or later) setenv command are recognized. See the AmigaDos 1.3
manual. The environment variables set with the old Manx Set command (before
version 5.0) are not recognized.
MS-DOS line separators
On MS-DOS-like systems (MS-DOS itself, Win32, and OS/2), Vim assumes that all
the vimrc files have <CR> <NL> pairs as line separators. This will give
problems if you have a file with only <NL>s and have a line like
":map xx yy^M". The trailing ^M will be ignored.
Vi compatible default value
When Vim starts, the 'compatible' option is on. This will be used when Vim
starts its initializations. But as soon as:
- a user vimrc file is found, or
- a vimrc file in the current directory, or
- the "VIMINIT" environment variable is set, or
- the "-N" command line argument is given, or
- the "--clean" command line argument is given, or
even when no vimrc file exists.
- the defaults.vim script is loaded, or
- gvimrc file was found,
then it will be set to 'nocompatible'.
Note that this does NOT happen when a system-wide vimrc file was found.
This has the side effect of setting or resetting other options (see
'compatible'). But only the options that have not been set or reset will be
changed. This has the same effect like the value of 'compatible' had this
value when starting Vim.
'compatible' is NOT reset, and defaults.vim is not loaded:
- when Vim was started with the -u command line argument, especially with
"-u NONE", or
- when started with the -C command line argument, or
- when the name of the executable ends in "ex". (This has been done to make
Vim behave like "ex", when it is started as "ex")
But there is a side effect of setting or resetting 'compatible' at the moment
a .vimrc file is found: Mappings are interpreted the moment they are
encountered. This makes a difference when using things like "<CR>". If the
mappings depend on a certain value of 'compatible', set or reset it before
giving the mapping.
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.
Avoiding trojan horses
trojan-horse
While reading the "vimrc" or the "exrc" file in the current directory, some
commands can be disabled for security reasons by setting the 'secure' option.
This is always done when executing the command from a tags file. Otherwise it
would be possible that you accidentally use a vimrc or tags file that somebody
else created and contains nasty commands. The disabled commands are the ones
that start a shell, the ones that write to a file, and ":autocmd". The ":map"
commands are echoed, so you can see which keys are being mapped.
If you want Vim to execute all commands in a local vimrc file, you
can reset the 'secure' option in the EXINIT or VIMINIT environment variable or
in the global "exrc" or "vimrc" file. This is not possible in "vimrc" or
"exrc" in the current directory, for obvious reasons.
On Unix systems, this only happens if you are not the owner of the
vimrc file. Warning: If you unpack an archive that contains a vimrc or exrc
file, it will be owned by you. You won't have the security protection. Check
the vimrc file before you start Vim in that directory, or reset the 'exrc'
option. Some Unix systems allow a user to do "chown" on a file. This makes
it possible for another user to create a nasty vimrc and make you the owner.
Be careful!
When using tag search commands, executing the search command (the last
part of the line in the tags file) is always done in secure mode. This works
just like executing a command from a vimrc/exrc in the current directory.
If Vim startup is slow
slow-start
If Vim takes a long time to start up, use the --startuptime argument to find
out what happens. There are a few common causes:
- If the Unix version was compiled with the GUI and/or X11 (check the output
of ":version" for "+GUI" and "+X11"), it may need to load shared libraries
and connect to the X11 server. Try compiling a version with GUI and X11
disabled. This also should make the executable smaller.
Use the -X command line argument to avoid connecting to the X server when
running in a terminal.
- If you have "viminfo" enabled, the loading of the viminfo file may take a
while. You can find out if this is the problem by disabling viminfo for a
moment (use the Vim argument "-i NONE", -i). Try reducing the number of
lines stored in a register with ":set viminfo='20,<50,s10". viminfo-file.
Intro message
:intro
When Vim starts without a file name, an introductory message is displayed (for
those who don't know what Vim is). It is removed as soon as the display is
redrawn in any way. To see the message again, use the ":intro" command (if
there is not enough room, you will see only part of it).
To avoid the intro message on startup, add the 'I' flag to 'shortmess'.
info-message
The --help and --version arguments cause Vim to print a message and then
exit. Normally the message is sent to stdout, thus can be redirected to a
file with:
vim --help >file
From inside Vim:
:read !vim --help
When using gvim, it detects that it might have been started from the desktop,
without a terminal to show messages on. This is detected when both stdout and
stderr are not a tty. This breaks the ":read" command, as used in the example
above. To make it work again, set 'shellredir' to ">" instead of the default
">&":
:set shellredir=>
:read !gvim --help
This still won't work for systems where gvim does not use stdout at all
though.
神棍汉化组:
初始化
本节介绍Vim的非GUI版本。有关启动gui时的附加初始化,请参阅gui-fork。
在启动时,vim检查环境变量、文件和相应的参数。vim按照以下顺序进行。
1.设置“shell”和“term”选项
如果环境变量SHELL存在,它将被用来设置“shell”选项。在MS-DOS和Win32中,如果没有设置环境变量SHELL,则使用COMSPEC变量。
如果环境变量TERM存在,它将被用来设置“term”选项。但是,“term”将在启动GUI时更改。
2.处理参数
查看启动vim的命令中的参数和文件名。为所有文件创建缓冲区(但尚未加载)。“-V”参数被用来显示或记录下一步的操作,这对于调试初始化非常有用。
3.从环境变量或文件执行Ex命令
环境变量被读取作为一个Ex命令行,多个命令必须用“|”或“<NL>”分隔开。
包含初始化命令的文件被称为“vimrc”。vimrc文件中的每一行都被作为Ex命令行执行。有时“vimrc”文件也被称为“exrc”文件。他们是同类型的文件(都是初始化配置文件),但“exrc”是Vi经常使用的,“vimrc”是Vim特定的名称。另见vimrc-into(vimrc简介文件)
个人初始化文件存放的位置:
Unix是$HOME/.vimrc或$HOME/.vim/vimrc
OS/2是$HOME/.vimrc, $HOME/vimfiles/vimrc或$VIM/.vimrc (or _vimrc)
MS-Windows是$HOME/_vimrc, $HOME/vimfiles/vimrc或$VIM/_vimrc
Amiga是s:.vimrc, home:.vimrc, home:vimfiles:vimrc或$VIM/.vimrc
vim按照上面指定的顺序搜索初始化配置文件,只读取找到的第一个文件。
建议:将所有Vim配置文件放在$HOME/.vim/目录下(MS-Windows则放在$HOME/vimfiles/目录下)。这使它很容易复制到另一个系统。....略....
4.加载插件
5.设置“shellpipe”和“shellredir”
6.如果使用“-n”命令参数,则将“updatecount”设置为零。
7.设置二进制选项。
8.执行GUI初始化
9.读取viminfo文件
10.读取quickfix文件
11.打开所有窗口
12.执行启动命令
翻译先放一放,下面有一篇大侠的文章。请看......
这个翻译实在是......
还好在网上找到一篇很好的文章“http://www.dengb.com/xtzh/743349.html”,这篇文章实在是写的太好了,我忍不住把它搬了过来。
实践中学习vim之vim配置文件、插件文件加载路径
0 引言
理解vim的启动过程对于增强使用vim的信心非常重要,本文所有的信息均来自vim自身提供的参考手册和作者实际操作实践。VIM REFERENCE MANUAL的Starting Vim这节详细描述了vim的启动过程。vim完整的启动过程非常复杂,因为要兼容不同的平台,不同的运行模式。本文只考虑Windows, Mac OS X, Linux平台上最常见的启动流程。
1 vim启动初始化流程
(1)设置内部变量shell和term
vim根据环境变量 $SHELL 和 $TERM 设置这两个内部变量(option).
(2)处理命令行参数
命令行参数包括选项和要打开的文件名,vim为每一个文件开辟内存空间。
(3)加载系统级别和用户级别的配置文件
(a) 根据编译时指定的路径,加载系统级别vimrc配置文件
(b) 根据编译时指定的路径或默认路径,加载用户级别的vimrc配置文件
(4)加载插件文件
根据runtimepath内部变量的值加载。所有runtimepath中的所有目录下名为plugin的子目录们下面所有以.vim结尾的文件都会被加载执行。
(5)设置shellpipe和shellredir内部变量
(6)如果命令行参数有-n,则设置updatecount内部变量
(7)如果命令行参数有-b,则设置二进制相关的多个内部变量
(8)执行GUI部分的初始化
(9)如果viminfo不为空,则读取指定的viminfo文件,恢复上次的编辑环境
(10)如果命令行参数有-q,则读取quickfix文件
(11)打开显示所有的窗口
(12)执行用户指定的启动时命令
可以看出,vim的启动初始化流程非常复杂。本文我们只关心配置文件的加载部分,这也是与大多数vim使用者直接相关的部分。从上面的流程看出:
vim加载系统级配置文件是根据编译时指定的路径加载的;
用户级配置文件则可以在编译时指定,也可以不指定,如果不指定,则使用默认值。默认值对于不同的平台是不同的。
对于unix平台:$HOME/.vimrc
对于Windows平台:$HOME/_vimrc,如果不存在,则使用$VIM/_vimrc
插件文件是根据runtimepath来确定路径的。
仔细分析就会发现,其实这里面还有很多不确定的东西: 可见除了编译时通过绝对路径指定的系统级配置文件vimrc之外的所有其他情况下的配置文件路径都严重依赖于环境变量的设置。问题是用户如果在运行vim之前没有设置这些环境变量,vim该如何是好呢?
2 配置文件路径的确定
2.1 编译时指定的路径
在编译vim源码的时候,需要指定各种配置文件的路径。如果最终用户不是当初执行编译的人如何知道这些信息呢?编译后的二进制vim可执行文件,自身包含了当初编译的时候指定的配置信息。我们可以通过执行vim --version来查看,如下是三个实例,一个是CentOS6.4下自带vim的编译时信息,另一个是Mac OS X 10.1系统自带vim的编译时信息,第三个是Windows8.1平台安装的vim官方7.4二进制版本。
【CentOS自带vim的编译时配置信息】
[root@localhost test]# vim --version
VIM - Vi IMproved 7.2 (2008 Aug 9, compiled Apr 5 2012 10:12:47)
Included patches: 1-411
Modified by <bugzilla@redhat.com>
Compiled by <bugzilla@redhat.com>
Huge version without GUI. Features included (+) or not (-):
+arabic +autocmd -balloon_eval -browse ++builtin_terms +byte_offset +cindent
-clientserver -clipboard +cmdline_compl +cmdline_hist +cmdline_info +comments
+cryptv +cscope +cursorshape +dialog_con +diff +digraphs -dnd -ebcdic
+emacs_tags +eval +ex_extra +extra_search +farsi +file_in_path +find_in_path
+float +folding -footer +fork() +gettext -hangul_input +iconv +insert_expand
+jumplist +keymap +langmap +libcall +linebreak +lispindent +listcmds +localmap
+menu +mksession +modify_fname +mouse -mouseshape +mouse_dec +mouse_gpm
-mouse_jsbterm +mouse_netterm -mouse_sysmouse +mouse_xterm +multi_byte
+multi_lang -mzscheme -netbeans_intg -osfiletype +path_extra +perl +postscript
+printer +profile +python +quickfix +reltime +rightleft -ruby +scrollbind
+signs +smartindent -sniff +startuptime +statusline -sun_workshop +syntax
+tag_binary +tag_old_static -tag_any_white -tcl +terminfo +termresponse
+textobjects +title -toolbar +user_commands +vertsplit +virtualedit +visual
+visualextra +viminfo +vreplace +wildignore +wildmenu +windows +writebackup
-X11 -xfontset -xim -xsmp -xterm_clipboard -xterm_save
system vimrc file: "/etc/vimrc"
user vimrc file: "$HOME/.vimrc"
user exrc file: "$HOME/.exrc"
fall-back for $VIM: "/usr/share/vim"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H -O2 -g -pipe -Wall -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i686 -mtune=atom -fasynchronous-unwind-tables -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_FORTIFY_SOURCE=1 -D_REENTRANT -D_GNU_SOURCE -fstack-protector -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/lib/perl5/CORE -I/usr/include/python2.6 -pthread
Linking: gcc -Wl,-E -Wl,-rpath,/usr/lib/perl5/CORE -L/usr/local/lib -o vim -lselinux -lncurses -lacl -lgpm -Wl,-E -Wl,-rpath,/usr/lib/perl5/CORE -fstack-protector -L/usr/local/lib -L/usr/lib/perl5/CORE -lperl -lresolv -lutil -lc -L/usr/lib/python2.6/config -lpython2.6 -lutil -lm -Xlinker -export-dynamic
[root@localhost test]#
【Mac OS X自带vim的编译时配置信息】
smstongtekiMac-mini:~ smstong$ vim --version
VIM - Vi IMproved 7.3 (2010 Aug 15, compiled Aug 24 2013 18:58:47)
Compiled by root@apple.com
Normal version without GUI. Features included (+) or not (-):
-arabic +autocmd -balloon_eval -browse +builtin_terms +byte_offset +cindent
-clientserver -clipboard +cmdline_compl +cmdline_hist +cmdline_info +comments
-conceal +cryptv +cscope +cursorbind +cursorshape +dialog_con +diff +digraphs
-dnd -ebcdic -emacs_tags +eval +ex_extra +extra_search -farsi +file_in_path
+find_in_path +float +folding -footer +fork() -gettext -hangul_input +iconv
+insert_expand +jumplist -keymap -langmap +libcall +linebreak +lispindent
+listcmds +localmap -lua +menu +mksession +modify_fname +mouse -mouseshape
-mouse_dec -mouse_gpm -mouse_jsbterm -mouse_netterm -mouse_sysmouse
+mouse_xterm +multi_byte +multi_lang -mzscheme +netbeans_intg -osfiletype
+path_extra -perl +persistent_undo +postscript +printer -profile +python/dyn
-python3 +quickfix +reltime -rightleft +ruby/dyn +scrollbind +signs
+smartindent -sniff +startuptime +statusline -sun_workshop +syntax +tag_binary
+tag_old_static -tag_any_white -tcl +terminfo +termresponse +textobjects +title
-toolbar +user_commands +vertsplit +virtualedit +visual +visualextra +viminfo
+vreplace +wildignore +wildmenu +windows +writebackup -X11 -xfontset -xim -xsmp
-xterm_clipboard -xterm_save
system vimrc file: "$VIM/vimrc"
user vimrc file: "$HOME/.vimrc"
user exrc file: "$HOME/.exrc"
fall-back for $VIM: "/usr/share/vim"
Compilation: gcc -c -I. -D_FORTIFY_SOURCE=0 -Iproto -DHAVE_CONFIG_H -arch i386 -arch x86_64 -g -Os -pipe
Linking: gcc -arch i386 -arch x86_64 -o vim -lncurses
smstongtekiMac-mini:~ smstong$
【Windows8.1 安装的vim官方二进制vim7.4编译配置信息】
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Aug 10 2013 14:33:40)
MS-Windows 32 位控制台版本
编译者 mool@tororo
大型版本 无图形界面。 可使用(+)与不可使用(-)的功能:
+arabic +ex_extra -mouseshape +tag_binary
+autocmd +extra_search +multi_byte +tag_old_static
-balloon_eval +farsi +multi_lang -tag_any_white
-browse +file_in_path -mzscheme -tcl
++builtin_terms +find_in_path -netbeans_intg -tgetent
+byte_offset +float +path_extra -termresponse
+cindent +folding -perl +textobjects
+clientserver -footer +persistent_undo +title
+clipboard +gettext/dyn -postscript -toolbar
+cmdline_compl -hangul_input +printer +user_commands
+cmdline_hist +iconv/dyn -profile +vertsplit
+cmdline_info +insert_expand -python +virtualedit
+comments +jumplist -python3 +visual
+conceal +keymap +quickfix +visualextra
+cryptv +langmap +reltime +viminfo
+cscope +libcall +rightleft +vreplace
+cursorbind +linebreak -ruby +wildignore
+cursorshape +lispindent +scrollbind +wildmenu
+dialog_con +listcmds +signs +windows
+diff +localmap +smartindent +writebackup
+digraphs -lua -sniff -xfontset
-dnd +menu +startuptime -xim
-ebcdic +mksession +statusline -xterm_save
+emacs_tags +modify_fname -sun_workshop -xpm_w32
+eval +mouse +syntax
系统 vimrc 文件: "$VIM\vimrc"
用户 vimrc 文件: "$HOME\_vimrc"
第二用户 vimrc 文件: "$HOME\vimfiles\vimrc"
第三用户 vimrc 文件: "$VIM\_vimrc"
用户 exrc 文件: "$HOME\_exrc"
第二用户 exrc 文件: "$VIM\_exrc"
编译方式: cl -c /W3 /nologo -I. -Iproto -DHAVE_PATHDEF -DWIN32 -DFEAT_CSCOPE -DWINVER=0x0400 -D_WIN32_WINNT=0x0400 /Fo.\ObjCi386/ /Ox /GL -DNDEBUG /Zl /MT -DDYNAMIC_ICONV -DDYNAMIC_GETTEXT -DFEAT_BIG /Fd.\ObjCi386/ /Zi
链接方式: link /RELEASE /nologo /subsystem:console /LTCG:STATUS oldnames.lib kernel32.lib advapi32.lib shell32.lib gdi32.lib comdlg32.lib ole32.lib uuid.lib /machine:i386 /nodefaultlib libcmt.lib user32.lib /PDB:vim.pdb -debug
可以看出,不同版本的vim,其编译时指定的配置文件路径并不相同。除了CentOS平台的vim使用绝对路径指定/etc/vimrc为其系统配置文件以外,其他的平台及配置文件均依赖于环境变量$VIM和$HOME。
2.2 环境变量的确定步骤
先来看$VIM,大部分用户在使用vim前并没有手动去设置这个环境变量,而vim仍然正确的找到了配置文件,这是如何做到的呢?vim内部按照如下顺序查找或者定义$VIM,一旦有一个步骤成功,那么后面的步骤就会忽略掉。
(1)如果操作系统平台定义了$VIM环境变量,则直接使用;
(2)如果helpfile变量的值不包含其他的环境变量,则使用这个变量值来确定。实际上helpfile的默认值是$VIMRUNTIME/doc/help.txt,也就是说包含一个环境变量,所以默认情况下不能通过helpfile来确定。
(3)对于Windows平台,vim使用自身的可执行文件所在的位置来确定。我们前面Windows平台的例子中,vim就是在这一步确定的$VIM,其值为:VIM=C:\Program Files (x86)\Vim。
对于*inx平台,使用编译时指定的安装路径来确定(也就是前面vim --version结果中显示的"fall-back for $VIM" 。前面Mac OS X和CentOS平台的vim都是在这一步确定的$VIM,其值均为VIM=/usr/share/vim。
再来看$VIMRUNTIME。这个环境变量一般不需要用户去设置,而是让vim自身去猜测。下面是猜测步骤:
(1)如果用户定义了$VIMRUNTIME环境变量,直接使用;
(2)如果$VIM/vim{版本号}这个路径存在,那么使用它作为$VIMRUNTIME的值;
(3)如果$VIM/runtime存在,使用它作为$VIMRUNTIME的值;
(4)使用$VIM的值作为$VIMRUNTIME的值,这是vi时期的兼容模式;
(5)如果helpfile内部变量不包含环境变量,则使用helpfile来推导$VIMRUNTIME。
对于我们前面的三个平台,都是在第(2)步骤确定了$VIMRUNTIME的值。
最后来看$HOME,这个对于unix类环境来说,一般都会设置的,无需多说。
3 在实践中使用配置文件
通过前面的分析,我们弄清楚了vim配置文件及插件文件的加载时机与加载路径。接下来就可以根据这些知识来定制属于我们自己的配置了。
3.1 添加或修改配置文件
配置文件的主要作用是修改vim的默认行为以满足个性化需求,也就是修改vim内部变量的默认值。vim分为系统级配置文件和用户级配置文件。vim的手册推荐用户自定义的配置放入用户自定义文件中。我们以CentOS平台下的vim为例。用户级配置文件路径为:$HOME/.vimrc。在这个文件里增加如下代码:
set nu
set tabstop=4
set autoindent
这样我们以当前账户运行vim的时候,就会总是显示行号,tab键相当于4个空格的宽度,自动缩进。
3.2 添加插件文件
插件文件的主要作用是增强vim的功能,也就是创造新的功能而不是修改已有的功能。从前面的分析,我们知道插件文件可以存在很多路径下。我们以/usr/share/vim/vim72/plugin目录下为例。在这个目录下随便新建一个文本文件test.vim。内容如下:
nmap <F10> ggODate:<Esc>:read !date<CR>kJ$
这样,启动vim后,就可以通过F10快捷键直接在首行输入当前的日期信息。
其实vim配置文件和插件文件所支持的语法完全一样,只是人为的按照功能作用分开存储了,两者都支持vim的专用脚本语言VimScript,关于vimscript的使用超出了本文的范畴,作者本人也尚未掌握,以后学习的时候再做记录。
<完>
总结一下:
上面的文章讲的很明白了。虽然vim的初始化很复杂,但我们只需要知道我们关心的东西,其他的......等以后再去了解吧!
首先,电脑上得安装了vim。如果没有,就用命令先安装。
$ sudo apt-get install vim
安装了vim后我们就先来讨论vim的启动及运行程序:
那么,我们要去哪里下载插件呢?当然是GitHub了!网址是“https://github.com/”。
假如我们要安装NERDTree这个插件,那么就在右上角的搜索框里输入nerdtree进行搜索。
上图标记处就是NERDTree的下载地址了,那要怎么下载呢?别急!往下拉,其实网页中已经告诉我们了。这里的“教程”要比网上的其他的博文、论坛一类的“教程”要实在的多。我们大可以在这个地方接受第一手的学习,这样要比去别人博客里看现成的文章要好得多。下面我们一起来看看NERDTree的一些安装说明。
The NERDTree
Introduction
NERDTree的简介
The NERDTree is a file system explorer for the Vim editor. Using this plugin,users can visually browse complex directory hierarchies, quickly open files forreading or editing, and perform basic file system operations.NERDTree是Vim编辑器的文件系统资源管理器。通过这个插件,用户可以直观的浏览复杂的目录层次结构,快速打开文件进行读取或编辑,并执行基本的文件系统操作。
This plugin can also be extended with custom mappings using a special API. Thedetails of this API and of other NERDTree features are described in theincluded documentation.
此插件还可以使用特殊的API通过自定义映射进行扩展。
Installation
安装,终于到了我们期待的一步了。
Below are just some of the methods for installing NERDTree. Do not follow all of these instructions; just pick your favorite one. Other plugin managers exist, and NERDTree should install just fine with any of them.
以下只是安装NERDTree的一些方法。在这些方法中挑一个你最喜欢的就行了。如果你有插件管理器(比如:Vundle.vim或者vim-plug等等),那么用插件管理器来安装NERDTree会更好、更简单、也更轻松一点。本文主要是记录手动安装vim插件的过程,不讨论vim插件管理器,当然,后面我会再用一篇文章来记录vim插件管理器的安装和使用。
Vim 8+ packages
If you are using VIM version 8 or higher you can use its built-in package management; see :help packages for more information. Just run these commands in your terminal:
git clone https://github.com/preservim/nerdtree.git ~/.vim/pack/vendor/start/nerdtreevim -u NONE -c "helptags ~/.vim/pack/vendor/start/nerdtree/doc" -c q
如果你使用的是vim版本8或者更高版本,则可以使用其内置包管理;你可以在vim的命令模式下输入“:help packages ”来查看更多的信息。这种情况只需要在终端里输入以下命令来安装NERDTree插件:
$ git clone https://github.com/preservim/nerdtree.git ~/.vim/pack/vendor/start/nerdtreevim -u NONE -c "helptags ~/.vim/pack/vendor/start/nerdtree/doc" -c q
Otherwise, these are some of the several 3rd-party plugin managers you can choose from. Be sure you read the instructions for your chosen plugin, as there typically are additional steps you nee d to take.