Is it possible to include external files to be sourced in the vimrc file?
Is it as simple a matter as just adding source somefile
?
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityUnlike :source
, :runtime
doesn't need an absolute path to work. It is much better than :source
for building portable setups.
See :help :runtime
.
In the following example, we tell Vim to look for a vimrc
file in the directories given by the runtimepath
variable. Since ~/.vim/
is the first directory in the list, runtime vimrc
works just like :source ~/.vim/vimrc
without the need to provide a system-dependent path.
In ~/.vimrc
:
runtime vimrc
The answer should depend on what you want to scatter in several files. There are a few different ways to have a configuration in several files. Note: the difference between runtime
and source
has been described, I won't say anything again on this subject.
So. You'll also have to take into account the what. Nowadays, we seldom have a need to use so
or ru
from .vimrc. Yet, I still do it in only two cases:
For the other cases:
In the end, I have in my .vimrc:
:set
@romainl's answer is the best practice. But as an additional example I have these lines in my vimrc which might interest you. I use them to source a local vimrc which I don't keep in my dotfiles source control:
" Source a local vimrc {{{
if has('win32')
let $MYLOCALVIMRC = $HOME . "/_local.vim"
else
let $MYLOCALVIMRC = $HOME . "/.local.vim"
endif
if filereadable($MYLOCALVIMRC)
source $MYLOCALVIMRC
endif
" }}}
The first part which defines the file name to source may not be interesting for you but the second part which checks if the file can be sourced before sourcing it could be interesting.
Note that :h source
states:
Read Ex commands from {file}. These are commands that start with a ":"
So this will allow you to source a file containing vimscript but you can't source directly a file containing another language with this method, even if the first line contains the right shebang (#!/bin/...
)