Loading scripts in vimrc safely

From Vim Tips Wiki

Jump to: navigation, search
Tip 334 Printable Monobook Previous Next

created September 30, 2002 · complexity basic · author piaip · version 6.0


If you copy your vimrc to many places, you might get a problem. Writing a portable vimrc is sometimes painful. Maybe in some places you have latest Vim 6.1, and in some places you have Vim 5.x. And maybe you have some favorite scripts in your own computer while other places does not.

Here're some tips and examples on writing portable vimrc that will not make error messages when environment changes:

(1) Check version:

if version >= 600
 set foldcolumn=2
endif

Note: version 6.1 would be 601

(2) Use environment variables:

source $VIMRUNTIME/vimrc_example.vim

(3) Loading scripts(especially useful for scripts just merged into standard macros):

if filereadable($VIMRUNTIME . "/macros/matchit.vim")
 source $VIMRUNTIME/macros/matchit.vim
endif

[edit] Comments

Regarding sourcing stuff inside the runtime directory such as the macros stuff, look at the :runtime command -- it doesn't require error checking (though it might be only for 6.x or above so pop it inside the version check).


Personal tools