简体   繁体   English

.vim脚本/插件/ whatelse中的.vimrc绝对路径或相对路径

[英].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". 想象一下,你必须以root身份在一个文件上工作,但你想要自己的.vimrc,而不需要调用“-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). 我把它称为“realuser.vim”,并在root的.vimrc(/root/.vimrc)中“源”它。

If you login to your server now via SSH or on ubuntu via Gnome, you go "su -" and login as root. 如果您现在通过SSH或通过Gnome在ubuntu上登录您的服务器,您可以“su - ”并以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". 该脚本检测到登录到该计算机的真实用户是“yourlogin”。 Then it checks if in /home/yourlogin/ a file ".vimrc" is existent. 然后检查是否存在/ home / yourlogin /文件“.vimrc”。 So, it is and it loads it. 所以,它是,它加载它。

My problem is, that in /home/yourlogin/.vimrc is the following line: 我的问题是,在/home/yourlogin/.vimrc中是以下行:

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 /root/.vimrc加载/home/yourlogin/.vimrc,因此检入/root/.vim/plugin/someplugin.vim,这是不存在的,因为它只在/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/? 我怎样才能使用相对路径或类似的东西告诉vim源文件只在/home/yourlogin/.vim/?

The relative equivalent to :source is :runtime . 相对等效于:source: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. 有了这个,只要你还调整'runtimepath'选项中的路径,它应该工作。

Alternatively, you could also change the value of $HOME inside Vim; 或者,您也可以在Vim中更改$HOME的值; this affects the expansion of ~ , too: 这也影响了~的扩展:

:let $HOME = '/home/yourlogin'

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 . 你可能在这个过程中学到了很多,所以我不会把它称为浪费时间,但是...解决你的实际问题只是使用sudoedit See $ man sudo . $ man sudo

Also, if you need escalated privileges for extended editing sessions you should probably take a moment to revise your setup/workflow. 此外,如果您需要升级的编辑会话权限,您可能需要花一点时间来修改您的设置/工作流程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM