Learn vim's vim configuration file and plug-in file loading path in practice

Source: Internet
Author: User

0 Introduction

Understanding the vim startup process is very important to enhance the confidence in using vim. All the information in this article comes from the reference manual provided by vim and the actual operation practices of the author. The Starting VIM section of Vim reference manual describes in detail the START process of vim. The complete Startup Process of vim is very complicated because it must be compatible with different platforms and different running modes. This article only takes into account the most common startup procedures on Windows, Mac OS X, and Linux platforms.

1. vim starts the initialization process.

(1) set the internal variables shell and term

Vim sets these two internal variables (option) based on the environment variables $ SHELL and $ TERM ).

(2) process command line parameters

Command line parameters include options and file names to be opened. vim opens up memory space for each file.

(3) load system-level and user-level configuration files

(A) load the system-level vimrc configuration file based on the path specified during compilation

(B) load the user-level vimrc configuration file based on the path or default path specified during compilation.

(4) load the plug-in file

Load data according to the value of the runtimepath internal variable. All the files ending with. vim under the subdirectory named plugin in all runtimepath directories will be loaded and executed.

(5) set shellpipe and shellredir internal variables

(6) If the command line parameter has-n, set the internal variable of updatecount.

(7) If the command line parameter has-B, multiple internal variables related to binary are set.

(8) initialize the GUI.

(9) If viminfo is not empty, read the specified viminfo file and restore the last editing environment.

(10) If the command line parameter has-q, read the quickfix file.

(11) Open and show all windows

(12) run the user-specified startup command

It can be seen that the process of starting and initializing vim is very complicated. In this article, we only care about the loading part of the configuration file, which is directly related to most vim users. From the process above, we can see that:

  • Vim loads the system-level Configuration File Based on the path specified during compilation;
  • User-level configuration files can be specified during compilation or not. If they are not specified, the default value is used. The default value is different for different platforms.
    • For unix platforms: $ HOME/. vimrc
    • For Windows: $ HOME/_ vimrc, use $ VIM/_ vimrc if it does not exist.
  • The plug-in file is determined based on runtimepath.
After careful analysis, we will find that there are still many uncertainties: it can be seen that all the configuration file paths except the system-level configuration file vimrc specified by the absolute path during compilation depend heavily on the environment variable settings. The problem is, if you haven't set these environment variables before running vim, what should vim do?

2. Determination of the configuration file path 2.1 The path specified during compilation

When compiling the vim source code, you must specify the paths of various configuration files. If the end user is not the person who originally compiled the code, how can he know the information? The compiled binary vim executable file contains the configuration information specified at the time of compilation. Run vim -- version to view the following three instances. One is the built-in vim compilation information under CentOS6.4, the other is the vim Compilation Time Information provided by Mac OS X 10.1, and the third is the official vim 7.4 binary version installed on Windows.

[Configuration of vim during compilation in CentOS]

[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]#

[Configuration of vim during compilation in Mac OS X]

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$

[Vim official binary vim7.4 compilation configuration information installed in Windows8.1]

VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Aug 10 2013 14:33:40)
MS-Windows 32-bit console version
Compiled by Moore @ Tororo
Large versions have no graphical interface. Functions that can be used (+) and cannot be used (-):
+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
System vimrc file: "$VIM \ vimrc"
User vimrc file: "$home \ \
Second user vimrc file: "$home \ vimfiles \ vimrc"
Third user vimrc file: "$VIM \ \
User exrc file: "$home \ \
Second user exrc file: "$VIM \ \
Compilation method: 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 mode: 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

We can see that the path of the configuration file specified during compilation is different for vim of different versions. Except that vim on the CentOS platform uses the absolute path to specify/etc/vimrc as its system configuration file, other platforms and configuration files depend on the environment variables $ VIM and $ HOME.

2.2 steps for determining Environment Variables

First, let's take a look at $ VIM. Most users did not manually set this environment variable before using vim, but vim still correctly found the configuration file. How does this happen? Vim internally searches for or defines $ VIM in the following order. Once one step succeeds, the subsequent steps will be ignored.

(1) If the operating system platform defines $ VIM environment variables, use them directly;

(2) If the value of the helpfile variable does not contain other environment variables, use this variable value to determine. In fact, the default value of helpfile is $ VIMRUNTIME/doc/help.txt, which contains an environment variable. Therefore, it cannot be determined by default through helpfile.

(3) For Windows platforms, vim uses its own executable file location to determine. In the preceding Windows platform example, vim is $ VIM identified in this step. Its value is VIM = C: \ Program Files (x86) \ Vim.

For * idea platform, use the installation path specified during compilation to determine (that is, the "fall-back for $ vim" shown in the preceding VIM -- version result ". In the previous steps, both the Mac OS X and the vim ON THE CentOS platform determine $ VIM in this step. The values are VIM =/usr/share/vim.

Let's take a look at $ VIMRUNTIME. Generally, this environment variable does not need to be set by the user, but can be guessed by vim itself. The following is a guess:

(1) If you have defined the $ VIMRUNTIME environment variable, use it directly;

(2) If the path $ VIM/vim {version} exists, use it as the value of $ VIMRUNTIME;

(3) If $ VIM/runtime exists, use it as the value of $ VIMRUNTIME;

(4) use the value of $ VIM as the value of $ VIMRUNTIME, which is the compatibility mode of the vi period;

(5) If the internal variables of helpfile do not contain environment variables, use helpfile to derive $ VIMRUNTIME.

For the preceding three platforms, the $ VIMRUNTIME value is determined in step (2.

Finally, let's take a look at $ HOME. This is usually set for unix environments.

3. Using the configuration file in practice, we found out the loading time and path of the vim configuration file and plug-in file. Next, we can customize our own configurations based on this knowledge. 3.1 The main function of adding or modifying the configuration file is to modify the default behavior of vim to meet Personalized Requirements, that is, to modify the default value of vim internal variables. Vim is divided into system-level configuration files and user-level configuration files. In the vim manual, we recommend that you add custom configurations to your custom files. We use vim on the CentOS platform as an example. User-level configuration file path: $ HOME/. vimrc. Add the following code to this file:
 set nu
  set tabstop=4
  set autoindent


In this way, when running vim with the current account, the row number is always displayed. The tab key is equivalent to the width of four spaces and is automatically indented. 3.2 The main function of adding a plug-in file is to enhance the vim function, that is, to create new functions rather than modifying existing functions. From the previous analysis, we know that the plug-in file can exist in many paths. Take the directory/usr/share/vim/vim72/plugin as an example. Create a text file test. vim in this directory. The content is as follows:
nmap <F10> ggODate:<Esc>:read !date<CR>kJ$
In this way, after vim is started, you can enter the current date in the first line through the F10 shortcut.
In fact, vim configuration files and plug-in files support the same syntax, but are manually stored separately based on functions. Both support vim's dedicated scripting language VimScript, the use of vimscript is beyond the scope of this article, and the author has not mastered it, and will record it later.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.