Use another user's .vimrc and .vim/

6

6

I know that I can :source /path/to/some/.vimrc to load my user's .vimrc when running as root, but how can I use the entire .vim/ directory as well? Is there a startup option for using /home/user/.vim/ that I can set up as an alias?

And where in the fine manual would I have found this information if I had know what to look for. A simple :help .vim or :help .vimrc did not find the information regarding using the entire .vim/ direcotry (-U is only for the .vim file).

Thanks.

dotancohen

Posted 2012-04-18T14:55:28.013

Reputation: 9 798

:help vimruntime in this case. – Daniel Andersson – 2012-04-18T15:04:22.793

Answers

1

use the environment variable VIMINIT to point to the right .vimrc and VIMRUNTIME to point to the right .vim-directory. you can do this via env:

$> env VIMINIT=/home/user/.vimrc VIMRUNTIME=/home/user/.vim/ vim

$> VIMINIT='let $MYVIMRC = expand('\''~user'\'') . '\''/.vimrc'\''|source $MYVIMRC' vim -c 'set runtimepath=~user/.vim,/var/lib/vim/addons,$VIM/vimfiles,$VIMRUNTIME,$VIM/vimfiles/after,/var/lib/vim/addons/after,~user/.vim/after'

as always: if thats too long to type, create a shell-function or a shell-alias.

to read more about how vim launches and what happens behind the scenes, read :help initialization.

akira

Posted 2012-04-18T14:55:28.013

Reputation: 52 754

1Thank you akira! This is exactly what I need, and thank you for the man page reference. – dotancohen – 2012-04-18T15:55:37.893

11

It seems that VIMINIT and VIMRUNTIME are not used as akira said.

VIMINIT is used as an Ex command, so VIMINIT=/home/user/.vimrc would try a search and give an errror like:

search hit BOTTOM, continuing at TOP
Error detected while processing VIMINIT:
E486: Pattern not found: home

VIMRUNTIME normally point to the location where vim's basic support files is installed, like /usr/share/vim/vim73, so if it's redirected to /home/user/.vim, vim would lost many basic functions unless your vim is just installed to /home/usr/.vim.


According to :help -u and :help vimrc, -u vimrc option can specify the .vimrc file but will skip most other initialization files, like system vimrc, eg. /etc/vimrc. If the specified vimrc file does almost all jobs, then the shortcomming, if called this, is trivial.

According to :help runtimepath, pathes of runtimepath will be searched for support files, so we can prepend /home/user/.vim and append /home/user/.vim/after to runtimepath before soucing vimrc file using --cmd options at startup.

To sum up, we can set up this alias to use ~/.vimrc and ~/vim:

alias vim='vim --cmd "set runtimepath^=/home/user/.vim" \
               --cmd "set runtimepath+=/home/user/.vim/after" \
               -u /home/user/.vimrc'

ps. Alternatively, two simple symbolic links may also work for you.

mv /root/.vimrc{,.bak}
mv /root/.vim{,.bak}
ln -s /home/user/.vimrc /root/
ln -s /home/user/.vim /root/

Dot Cink

Posted 2012-04-18T14:55:28.013

Reputation: 211

2Thank you for that answer, I had the exact problem you had with the E486 error. But your answer got me thinking : wouldn't it be possible to set VIMINIT=":source /home/user/.vimrc" and just be done with it? At least, it seems to work for me... – user43791 – 2015-02-13T22:22:45.140

1Even better, you can modify the runtimepath as you indicated in the same command : VIMINIT=":set runtimepath^=/home/user.vim|:source /home/user/.vimrc". It works like a charm for me! :) – user43791 – 2015-02-13T22:32:41.843

@user43791 your method works like a charm for me as well! :) – j5shi – 2018-04-24T12:21:03.787

I've been searching for this solution for quite sometime and this is the most definitive answer I have come across. – Francis – 2014-03-28T16:34:00.883

1

Probably the best solution ever.

export MYVIMRC="/xxx/.vimrc"
export VIMINIT=":set runtimepath+=/xxx/.vim|:source $MYVIMRC"

where xxx is the customized path.

j5shi

Posted 2012-04-18T14:55:28.013

Reputation: 111