The Computer Oracle

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

--------------------------------------------------
Become or hire the top 3% of the developers on Toptal https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game Looping

--

Chapters
00:00 Question
01:23 Accepted answer (Score 42)
01:43 Answer 2 (Score 7)
02:15 Answer 3 (Score 5)
03:17 Answer 4 (Score 5)
03:36 Thank you

--

Full question
https://superuser.com/questions/224607/h...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#vim #vimrc

#avk47



ACCEPTED ANSWER

Score 44


in your main .vimrc file:

source otherVimScriptFilePath.vim

then just put your variable statement in that file:

" otherVimScriptFilePath.vim
let whoami = "user1"



ANSWER 2

Score 5


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




ANSWER 3

Score 5


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.




ANSWER 4

Score 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