Note that as a workaround you can explicitly specify a vimrc file when you launch with vim -u <vimrc>
. But I'm sure you want to find a more permanent "fix".
I can't tell you exactly what is going on with your system except that it's likely related to environment differences between sudo and non-sudo runs. Here's some info to help you investigate further...
Start by looking at :help vimrc
. This describes how vim
finds vimrc files. It's system dependent (e.g. Unix looks in $HOME/.vimrc
first, Windows $HOME/_vimrc
). You'll want to check, for example, the value of $HOME
when running with/without sudo
.
To get some ideas about what is being loaded you can run :scriptnames
from vim. This shows all scripts that have been loaded, in order. Look near the top of the output as that's usually where loading of your vimrc file appears.
You can also do vim -Vn
which will print out useful information depending on value of n:
>= 1 When the viminfo file is read or written.
>= 2 When a file is ":source"'ed.
>= 5 Every searched tags file and include file.
>= 8 Files for which a group of autocommands is executed.
>= 9 Every executed autocommand.
>= 12 Every executed function.
>= 13 When an exception is thrown, caught, finished, or discarded.
>= 14 Anything pending in a ":finally" clause.
>= 15 Every executed Ex command (truncated at 200 characters).
Update: minnmass' helpful answer led me to a fact I wasn't aware of. I knew that sudo -e
/sudoedit
helps protect against accidental clobbering of critical files by using temp files but I didn't know this: unlike most commands run by sudo, the editor is run with the invoking user's environment unmodified. So you may be able to keep $HOME/.vimrc
and do sudo -e <file>
. Give it a try if you are so inclined. (Depending on exiting values, you may first need to do export ENVVAR=vim
where ENVVAR
is any one of SUDO_EDITOR
, VISUAL
or EDITOR
.)