How can I make .vimrc read from an external file?

27

7

I'd like to modify my .vimrc to read the value of a variable from an external file. How can I do this?

Specifically, a friend and I share a git repo with our .vim files, but there are a few small differences in what we want in our configs. So most of the file is common, but we use if statements to determine whether to load user-specific sections, like this:

let whoami = "user2"
if whoami == "user1"
...

After checking our common .vimrc out of source control, we each have to change the let whoami assignment so our own section will be loaded. Instead, I'd like to keep a separate file, which can be different for each of us, and from which vim will load that variable value.

Maybe another angle on this is: Will vim automatically read all the files in my .vim directory? If so, we could each put a symlink in there called username.vim, and link that to an external file that would be different for each of us.

Nathan Long

Posted 2010-12-21T18:57:17.903

Reputation: 20 371

Answers

36

in your main .vimrc file:

source otherVimScriptFilePath.vim

then just put your variable statement in that file:

" otherVimScriptFilePath.vim
let whoami = "user1"

Robert S Ciaccio

Posted 2010-12-21T18:57:17.903

Reputation: 1 470

2FYI - In my situation, it was helpful to do let whoami = "" right before the source line, so that if it fails to load the external file, the variable exists and I get the error message we had set up previously for the variable not being set. – Nathan Long – 2010-12-22T15:10:34.993

2@GorillaSandwich: I use this to source my vimrc on my windows machine since windows doesn't have symbolic links. I have an SVN folder that contains this and all my other dot-files. So I just have an _vimrc that sources the real one in the svn folder. – Robert S Ciaccio – 2010-12-22T17:01:45.070

@GorillaSandwich: see my answer for the silent! source part – akira – 2010-12-23T07:25:29.533

6

Using a try/catch

Since asking this question, I've come up with another use case for loading an external file: machine-specific tweaks.

Since I may or may not need to make tweaks on a given machine, I'm using a try/catch to load the external file; if there's no such file, I let it fail silently.

" If there are any machine-specific tweaks for Vim, load them from the following file.
try 
  source ~/.vimrc_machine_specific
catch
  " No such file? No problem; just ignore it.
endtry 

Nathan Long

Posted 2010-12-21T18:57:17.903

Reputation: 20 371

5

To answer the last question, files in ~/.vim are not automatically loaded, but all files in ~/.vim/plugin are.

Heptite

Posted 2010-12-21T18:57:17.903

Reputation: 16 267

2and some other places underneath ~/.vim (after, autoload) – akira – 2010-12-22T06:52:26.123

4

You can have your ~/.vimrc load another file using the :source command. For example, you could each put your unique commands in ~/.myvimrc and load those commands with

source ~/.myvimrc

Or, as you were thinking, you could each put your name in that file like this:

let user = "user1"

and then put this in your ~/.vimrc:

source ~/.myvimrc
if user == "user1"
    " do this
elseif user == "user2"
    " do that
else
    echo "Invalid user"
endif

Rather than put your names in files, though, you could use $USER as akira suggested, or set user using whoami, like this:

let user = substitute(system('whoami'), '\n', '', '')

The substitute() function is needed because the output of system() usually has a '\n' tacked on the end.

garyjohn

Posted 2010-12-21T18:57:17.903

Reputation: 29 085

whoami does not work on windows – akira – 2010-12-22T08:02:43.150

@akira: True, but GorillaSandwich is using Unix, not Windows. $USER doesn't work with Windows, either, at least not Windows XP. – garyjohn – 2010-12-23T06:24:11.893

just saying, on windows its $USERNAME. mainpoint is: whoami spawns a new process, when you open your editor it should be fast and not (useless) doing work several times again. – akira – 2010-12-23T07:24:15.633

2

the name of the logged-in user is available in the $USER environment variable. you can access that variable easily:

:echo $USER

so, just use

execute "silent! source vimrc." . $USER

in your vimrc and put your user specific settings into

vimrc.joe

or whatever your login-name is

akira

Posted 2010-12-21T18:57:17.903

Reputation: 52 754

0

I'm not sure what your vimrc looks like so this may not be feasible, but if there are large parts that you and your friend share, you might want to break them up into separate plugins. You can add files to the ~/.vim/plugin directory to have them loaded on startup. (They're loaded in alphabetical order -- use :scriptnames to check.)

That way you can each have a vimrc that's completely unique, but all of your common configuration are in plugins. If you break them up logically, then it makes it easy to share with others too.

idbrii

Posted 2010-12-21T18:57:17.903

Reputation: 682