in your main .vimrc file:
then just put your variable statement in that file:
" otherVimScriptFilePath.vim let whoami = "user1"
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
To answer the last question, files in
~/.vim are not automatically loaded, but all files in
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
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
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.
the name of the logged-in user is available in the $USER environment variable. you can access that variable easily:
so, just use
execute "silent! source vimrc." . $USER
in your vimrc and put your user specific settings into
or whatever your login-name is
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.