Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.vimrc absolute or relative path in .vim script/plugin/whatelse

I actually have the following situation and thats what working actually:

Imagine you have to work as root on a file but you want ur own .vimrc without calling "-u". So I started the following "plugin":

let g:realuser=system('w | grep $(ps w | grep ' . getpid() . ' | head -n1 | awk "{ print \$2 }") | awk "{ print \$1 }"')
if $USER == 'root'
    let g:vimrc=system('printf /home/%s/.vimrc '. g:realuser)
    if filereadable(g:vimrc)
        exec ":source " . g:vimrc
        finish
    endif
endif

I call it "realuser.vim" and "source" it in the root's .vimrc (/root/.vimrc).

If you login to your server now via SSH or on ubuntu via Gnome, you go "su -" and login as root. Then u change to your working directory and open the file. The script detects, that the real user who logged on to the machine is "yourlogin". Then it checks if in /home/yourlogin/ a file ".vimrc" is existent. So, it is and it loads it.

My problem is, that in /home/yourlogin/.vimrc is the following line:

source ~/.vim/plugin/someplugin.vim

So guess what. The /root/.vimrc loads the /home/yourlogin/.vimrc and therefore checks in /root/.vim/plugin/someplugin.vim which is not existent since it is only in /home/yourlogin/.vim

How can I use relative paths or sth like that to tell vim that the source file is only in /home/yourlogin/.vim/?

like image 446
boesing Avatar asked Oct 16 '13 11:10

boesing


1 Answers

The relative equivalent to :source is :runtime.

source ~/.vim/plugin/someplugin.vim

becomes

runtime plugin/someplugin.vim

With this, as long as you also adapt the paths in the 'runtimepath' option, it should work.

Alternatively, you could also change the value of $HOME inside Vim; this affects the expansion of ~, too:

:let $HOME = '/home/yourlogin'
like image 84
Ingo Karkat Avatar answered Sep 24 '22 14:09

Ingo Karkat