Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 6 down vote accepted

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'


: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.


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")'
share|improve this answer
thanks a bunch for taking the time to answer. This looks very promising. I'll tinker with this a little tomorrow and let you know how it goes. I wish my VimScript fu was stronger, but I am working on it and this points into a promising direction. – 0xC0000022L Mar 6 '13 at 3:13
@0xC0000022L: the way you showed it \\~ does the job. One escape for Bash, I reckon? Anyway this still doesn't work, because something is different about how VimScript handles the ~. – 0xC0000022L Mar 6 '13 at 19:52
I think I have it. Notice that :set rtp? shows "~/.vim" but that :echo &rtp expands ~ to the full path name. The following seems to work: `vim --cmd 'let &rtp = substitute(&rtp, $HOME."/\.vim", "alternate", "g")'. I'll update my answer. – garyjohn Mar 6 '13 at 20:02
thanks, very interesting and subtle difference. Makes some things more problematic for my specific case, but I'm going to accept. This got me a lot further. – 0xC0000022L Mar 7 '13 at 19:31

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.

share|improve this answer
This isn't exactly from the command line. Is there any way to make this dynamic or so? See, I suppose if I were to use something like $HOME then in my own account it'd resolve to /home/joe but as root still to /root ... so nothing is won. This .vimrc is supposed to work across platforms and Vim versions and setting an absolute path totally breaks that. Thanks for taking the time to answer, though. – 0xC0000022L Mar 6 '13 at 2:55
@0xC You are already specifying the vimrc from the command line, and this in turn specifies the runtime path. Note that in the vimrc you can run scripts to define variables, so if you edit your question with the exact outcome we can probably work out a way to do it. – Paul Mar 6 '13 at 2:58
edited my question. Thanks for the patience :) – 0xC0000022L Mar 6 '13 at 3:11
In the users .bashrc, could you set variable, say $REALHOME, that is exempt from the sudo reset_env and then use that as the basis for rtp? – Paul Mar 6 '13 at 3:16
It sounds like one viable option. I found it easier to smuggle variables this way, however: alias sudosu='sudo su -l root -c "REALHOME='\''/home/joe'\'' /bin/bash --rcfile /home/joe/.bashrc"' – 0xC0000022L Mar 6 '13 at 3:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.