5

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

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
boesing
  • 345
  • 3
  • 10

2 Answers2

11

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'
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • This works like a charm, but actually I still have some issues with the Filetype-Plugins. They are autoloaded by "filetype plugin" I guess, but it still loads /root/.vim/ftplugin/* instead of /home/yourlogin/.vim/ftplugin/*. So thats my last problem I guess. My solution for my first problem is `exec ":let $HOME = '" . system('printf /home/%s ' . g:realuser) . "'"` – boesing Oct 21 '13 at 10:26
  • vim documentation is horrible - but I don't think `runtime` is relative, it just searches through a specific set of folders. The principle of least surprise would be that you could source files relative to the location of the file it is being sourced from - but apparently - and frustratingly, that is not the case. – Casey May 23 '20 at 05:19
-1

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.

romainl
  • 186,200
  • 21
  • 280
  • 313
  • 1
    no, it is a waste of time - the lack of documentation targeted toward the average user who is not writing scripts or plugins does create an environment where people all over the world are wasting all kings of time. I got to this question trying to figure out how to source a document using a relative path (the principles of least surprise would make relative paths relative to the main config file, but apparently that is not the case). I like vim, but really have to question whether it is worth the time and frustration. – Casey May 23 '20 at 05:28