Learn how to determine the vim configuration 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 platforms: $ HOME/_ vimrc, if not, use the $ VIM/_ vimrc plug-in file to determine the path based on runtimepath. After careful analysis, we will find that there are still many uncertainties: the system-level configuration file path specified during compilation can be an absolute path or may contain $ VIM environment variables. The user-level configuration file path contains the environment variable $ HOME. The default value of runtimepath is as follows: $ HOME/for Unix platforms /. vim, $ VIM/vimfiles, $ VIMRUNTIME, $ VIM/vimfiles/after, $ HOME /. vim/after; for Windows platforms: $ HOME/vimfiles, $ VIM/vimfiles, $ VIMRUNTIME, $ VIM/vimfiles/after, $ HOME/vimfiles/after. 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 / lr / 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 comes with vim compile time configuration information]

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 on Windows8.1]

VIM-Vi IMproved 7.4 (2013 Aug 10, compiled Aug 10 2013 14:33:40)
MS-Windows 32-bit console version
Compiler mool @ tororo
Large version No graphical interface. Available (+) and unavailable (-) functions:
+ 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 \ _vimrc"
 Second user vimrc file: "$ HOME \ vimfiles \ vimrc"
 Third user vimrc file: "$ VIM \ _vimrc"
      User exrc file: "$ HOME \ _exrc"
  Second user exrc file: "$ VIM \ _exrc"
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 DFEAT_BIG /Fd.\ObjCi386/ / Zi
Link method: 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
It can be seen that the configuration file paths specified when compiling different versions of vim are different. Except for vim on the CentOS platform that uses absolute paths to specify / etc / vimrc as its system configuration file, other platforms and configuration files depend on the environment variables $ VIM and $ HOME. 2.2 Determination of environmental variables
Let's look at $ VIM first. Most users do not manually set this environment variable before using vim, and vim still finds the configuration file correctly. How does this work? Vim internally searches or defines $ VIM in the following order. Once one step is successful, the following steps will be ignored.

(1) If the operating system platform defines the $ VIM environment variable, it will be used 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 means that it contains an environment variable, so it cannot be determined by helpfile by default.

(3) For the Windows platform, vim uses the location of its own executable file to determine. In our previous Windows platform example, vim is the $ VIM determined in this step, and its value is: VIM = C: \ Program Files (x86) \ Vim.

For the * inx platform, use the installation path specified at compile time (that is, "fall-back for $ VIM" shown in the previous vim --version results. The previous vim for Mac OS X and CentOS platforms are determined at this step The value of $ VIM is VIM = / usr / share / vim.

Let's look at $ VIMRUNTIME again. This environment variable generally does not need to be set by the user, but allows vim to guess itself. Here are the guess steps:

(1) If the user defines the $ VIMRUNTIME environment variable, use it directly;

(2) If the path $ VIM / vim {version number} exists, then 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 in the vi period;

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

For the three platforms before us, the value of $ VIMRUNTIME was determined in step (2).

Finally, let's look at $ HOME, which is generally set for unix-like environments, needless to say.

3 Using configuration files in practice Through the previous analysis, we figured out the loading timing and loading path of vim configuration files and plug-in files. Then we can customize our own configuration based on this knowledge. 3.1 Add or modify the configuration file The main function of the configuration file is to modify the default behavior of vim to meet personalized requirements, that is, modify the default value of the internal variables of vim. Vim is divided into system-level configuration files and user-level configuration files. The vim manual recommends that user-defined configurations be placed in user-defined files. We take vim under the CentOS platform as an example. The user-level configuration file path is: $ HOME / .vimrc. Add the following code to this file:
  set nu
  set tabstop = 4
  set autoindent
In this way, when we run vim with the current account, the line number will always be displayed. The tab key is equivalent to the width of 4 spaces and is automatically indented. 3.2 Adding plug-in files The main function of plug-in files is to enhance the functions of vim, that is to create new functions instead of modifying existing functions. From the previous analysis, we know that the plugin file can exist in many paths. We take the / usr / share / vim / vim72 / plugin directory as an example. Create a new text file test.vim in this directory. The contents are as follows:
nmap <F10> ggODate: <Esc>: read! date <CR> kJ $
In this way, after starting vim, you can enter the current date information directly in the first line through the F10 shortcut key.
In fact, the syntax supported by the vim configuration file and the plug-in file is exactly the same, but it is artificially stored separately according to the function. Both of them support VimScript, a special scripting language of vim. The use of vimscript is beyond the scope of this article, and the author himself has not yet mastered , And make notes when studying 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.