19

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?

4 Answers 4

21

Unlike :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
6

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:

  • get sure a non autoload plugin gets loaded before the end of the .vimrc (in order to define other things that depends on this plugin)
  • load OS specific settings

For the other cases:

  • for function definitions, there are autoload plugins ;
  • for syntax highlighting, colorschemes, there are dedicated files in the eponymous directories ;
  • for filetype specific settings, there are filetype plugins (having autocommands in the .vimrc is a poor man solution, one that doesn't scale) ;
  • for project specific settings, there are plugins like local_vimrc ;
  • for any other set of complex mappings or features, there are plugins where we can regroup thematic definitions ;

In the end, I have in my .vimrc:

  • global options set with :set
  • the procedure needed by the plugin manager I'm using
  • various configurations for the plugins I use (I override the default keybindings in the .vimrc)
  • and a few things (too many actually) I've been dragging along for 20 years.
5

@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/...)

3

Yes, or even just so somefile. See :help so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.