.vimrc 配置文件在使用 'sudo' 编辑时不会加载 [英] .vimrc configuration file does not load when editing with 'sudo'
问题描述
我的 .vimrc 文件有问题,问题是它有时会被加载,有时不会.
I have problem with .vimrc file, the problem is that it sometimes get loaded, and sometimes not.
1 set number
2 syntax on
3 set autoindent
4 map <F2> :!g++ % -Wall -time -O<CR>
5 echo "it works!"
我添加了 echo 来检查它是否已加载,以及何时输入,例如vim .vimrc
,它被加载并在终端中显示它有效",但是当我输入例如sudo vim test.cpp
它没有被加载,消息没有显示.我正在使用 debian.
I've added echo to check if it's loaded, and when I type e.g. vim .vimrc
, it gets loaded and shows me "it works" in terminal, but when I type e.g. sudo vim test.cpp
it doesn't get loaded, the message doesn't show up. I'm using debian.
推荐答案
当您使用 sudo
时,Vim 会在不同的用户 (root
) 下启动.由于这个用户有一个不同的 home 目录,另一个 ~/.vimrc
被加载(或者没有,如果那个用户没有).您可以通过多种方式解决问题:
When you use sudo
, Vim gets launched under a different user (root
). As this user has a different home directory, another ~/.vimrc
is loaded (or none, if that user doesn't have one). You can solve the problem in multiple ways:
- 您可以直接指定
.vimrc
的位置:sudo vim -u $HOME/.vimrc
(不过这对插件没有帮助). - 您可以使用
sudo -e
或sudoedit
. - 你可以为 root 链接你的
.vimrc
(和.vim
插件目录):sudo ln -s $HOME/.vimrc .vimrc;sudo ln -s $HOME/.vim .vim
- 您可以将 root 的整个主目录更改为与您的相同(不推荐,因为安全和访问权限!)
- You can directly specify the location of your
.vimrc
:sudo vim -u $HOME/.vimrc
(this won't help with plugins, though). - You can use
sudo -e <file>
orsudoedit
. - You can symlink your
.vimrc
(and the.vim
plugins directory) for root:sudo ln -s $HOME/.vimrc .vimrc; sudo ln -s $HOME/.vim .vim
- You can change the entire home directory of root to be the same as yours (not recommended, because of security and access rights!)
这篇关于.vimrc 配置文件在使用 'sudo' 编辑时不会加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!