How to reference/source a custom .vimrc file
Solution 1
You can use the MYVIMRC
environment variable. This way, you won't have to pass -u
each time you fire up vim. (You can of course do an alias instead, but that won't help with e.g., vipw
)
Keep in mind that .vimrc
can execute arbitrary commands, if you use /home/user/.vimrc
you may be creating a security issue (e.g., someone manages to compromise your user account, changes your .vimrc, and then gets root the next time you edit a file as root). You can, of course, keep a known-safe copy in ~root/
somewhere.
You could assumably even set something up in ~root/.bashrc
to automatically set MYVIMRC
to something different for each different administrator.
Solution 2
Try -u
parameter and specify a path to an alternative configuration file.
For example: vim -u /home/jesse/myvimrc
See http://linuxcommand.org/lc3_man_pages/vim1.html
Solution 3
Use an alternate .vimrc file without plugins as mentioned, and add an alias in .bash_profile or something.
alias svim='vim -u ~/.vimrc_simple'
Really I prefer the following:
alias vvim='vim -u ~/.vimrc_vundle'
In order to keep vim
as a lightweight command, as plugin loading seems to slow down program instantiation.
Solution 4
In vim:
:source /path/to/your/.vimrc
Solution 5
I've only ever attempted this a few times and this seems to work fine for me. Define an alias for vim that is something like the following:
alias vim="HOME=~yournormaluser vim -c 'let \$HOME = \"$HOME\"'"
What this does is trick vim into using your $HOME/.vim/
environment, yet resets $HOME
from within vim so doing things like :e ~/something.txt
will still use the admin user's $HOME
.
This has the added advantage that you don't have to change the admin's ~/.vimrc
at all.
Mansoor Siddiqui
Updated on January 19, 2020Comments
-
Mansoor Siddiqui over 4 years
Is there a way to reference (or "source") another user's .vimrc file?
When I
kuu
(a variant ofsu
that uses kerberos security tokens) to an admin user ID, I would like to use my personal.vimrc
file.I don't want to overwrite the admin's existing
.vimrc
file because the admin ID is shared by multiple users. -
Randy Morris over 13 yearsThen again, this would alias vim for everyone who uses that admin user. When I've done this in the past I was in a situation where my environment was aware I was su'ing from my
$USER
and sourced~$USER/.aliases
so I could get away with this. Maybe you can still use this information somehow. -
Luc Hermitte over 13 yearsadd a randy_gvim alias, something the others are unlikely to use.
-
derobert over 13 yearsSourcing $USER/.aliases probably creates a security hole. See the note in my answer for an explanation.
-
Luc Hermitte over 13 yearsIt's not enough: the &rtp is not updated this way -- well, of course the .vimrc could reset the &rtp.
-
Randy Morris over 13 years@derobert: Indeed. In my case the 'admin' account was actually just a shared user account. For simply editing as root with an altered environment I'd almost always suggest
sudo -e
orsudoedit
. -
Mansoor Siddiqui over 13 yearsThanks for this, it's just what I asked. And I also appreciate the security warnings.
-
derobert over 9 years@Ruslan well, the question is about a .vimrc file, so... It won't work for EMACS either. Or joe, or nano, or Microsoft Word. (And if you call vim as
vi
, then you're asking it to run in compatibility mode) [I'm probably misunderstanding what you're trying to say; please clarify.] -
Tagar about 9 yearsvi also uses .vimrc. so I guess my comment is valid - author of the topic didn't specify if he is using vi or vim or both. ps. on some of the systems vi is linked to vim actually.
-
derobert about 9 years@Ruslan vi does not use .vimrc. vi is a completely separate program than vim, which reads
~/.exrc
. There are also the relatively popular nvi and elvis clones; neither of them read.vimrc
. Often, a version ofvim
is installed (or symlinked) tovi
(and which you'd use$EXINIT
to override). Though it actually still works fine, unless you have it symlinked tovim.tiny
(a version of Vim built to be as small file size as possible), which ignores$MYVIMRC
no matter what name you call it under. -
gmatht almost 8 yearsNot quite unfortunately.
man -u
:-u {vimrc} Use the commands in the file {vimrc} for initializations
. Sounds good so far, but it also puts vim in a special mode whichAll the other initializations are skipped.
This caused hard to debug problems for me which I tracked down tovim -u ~/vimrc
not being equivalent tovim
. What worked for me was:vim -u <(echo source /usr/share/vim/vimrc; cat ~/.vimrc)
using the bash shell specific<(...)
capture. -
Jorengarenar almost 3 yearsDisclaimer: In current versions of Vim, the
MYVIMRC
environmental variable is not read. Vim only sets it (ifvimrc
file has been found in one of hardcoded, specified locations, see:h .vimrc
)