Telling Vim to use custom .vimrc is easy, but how to tell it to use alternative path instead of ~/.vim?

I can use vim -u filename to use filename instead of my default .vimrc. I am using this method when switching user contexts (su) to use my .vimrc even though running as superuser.

How can I achieve the same also for the folder which by default is ~/.vim so that I can point Vim from the command line to an alternative folder?


What I want to achieve

Okay, assume user joe (HOME=/home/joe) and user root (HOME=/root). User joe has an alias set for vim which looks like this (but originally set using the $HOME variable to make this dynamic):

alias vim='vim -p -N -n -i NONE -u "/home/joe/.vimrc"'

Then user joe does something along the lines of sudo su - (but with added magic), resulting in the .bashrc and other goodies from /home/joe to be loaded for root. User root has now the exact same alias for vim set as shown above.

The problem is that this .vimrc is used on various systems and in various scenarios. Often Joe's account is called joe, but sometimes it'll be something like local.joe or whatever else, resulting in a different value for $HOME. So hard coding an absolute path to the ~/.vim folder doesn't seem to be a good idea. However, in our above scenario user root doesn't have a folder /root/.vim which, however, is expected to exist by default via the loaded .vimrc (/home/joe/.vimrc).

What I want to achieve - preferably on the command line - is to get Vim to use plugins etc from underneath /home/joe/.vim when started as root (assuming the alias is set as shown - other cases can be ignored). If there is some dynamic method via VimScript, please provide pointers. But using variables such as $HOME would lead to a catch 22, I think.


Solution 1:

Vim looks in ~/.vim because that directory is in the default list in 'runtimepath', abbreviated as 'rtp'. To tell Vim to look elsewhere, you will have to completely specify a different 'rtp' value, or edit the default using a call to substitute() for example. If you just want Vim to look in a different place for your configuration files first, don't care that it also looks in ~/.vim, and don't care that it doesn't look in your alternative after directory, the command is pretty simple:

vim --cmd 'set rtp^=alternate_dir'

See

:help --cmd
:help :set^=

Replacing .vim with your alternative directory takes a little more typing.

vim --cmd 'let &rtp = substitute(&rtp, "\.vim", "alternate", "g")'

I tried replacing ~/.vim with another path, but I couldn't match the ~, so I went ahead and posted what I had.

Edit

The reason I could not match the ~ in the value of 'rtp' is that when the value is obtained as the value of &rtp rather than the output of :set rtp?, the ~ is expanded to the full path name of the user's home directory. There is no ~ in the result.

The following works.

vim --cmd 'let &rtp = substitute(&rtp, $HOME."/\.vim", "alternate", "g")'

Solution 2:

In the .vimrc you choose, you can specify the runtime path:

let &runtimepath=/path/to/specific/vim/folder

If you are using your person .vimrc then you could set this to your personal .vim directory and it should work both as su and your normal account.