Question

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/?

Was it helpful?

Solution

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'

OTHER TIPS

You probably learned a lot in the process so I wouldn't call it a waste of time but… the solution to your actual problem is simply to use sudoedit. See $ man sudo.

Also, if you need escalated privileges for extended editing sessions you should probably take a moment to revise your setup/workflow.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top