Mansoor Siddiqui
Mansoor Siddiqui

Reputation: 21663

How to reference/source a custom .vimrc file

Is there a way to reference (or "source") another user's .vimrc file?

When I kuu (a variant of su 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.

Upvotes: 44

Views: 36778

Answers (9)

derobert
derobert

Reputation: 51147

Some distributions might add an extension to configuration (e.g. in $VIM/vimfiles) that source the file pointed by $MYVIMRC, if this environmental variable was set beforehand (normally, it's set by Vim internally after reading vimrc). 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 presumably even set something up in ~root/.bashrc to automatically set MYVIMRC to something different for each different administrator.

But again, keep in mind that is not regular Vim behaviour nor a very common practice. Better check documentation (:h initialization and :h VIMINIT) to learn about more "sanctioned" ways of changing the location of vimrc file.

Upvotes: 27

boesing
boesing

Reputation: 345

I've created a plugin years ago since I wanted my own .vimrc on our servers without forcing any user who uses sudo to navigate as root to my .vimrc.

My plugin works exactly as the plugin what @ULick provided.

GIST

Upvotes: 0

Jesse Vogt
Jesse Vogt

Reputation: 16519

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

Upvotes: 31

ULick
ULick

Reputation: 999

Method 2 - as an addition to .vimrc

tried different things
tty does not work, and system("who am i") neither (they come up empty when used from within vim-function), so this way is much longer. Any shortcuts are welcome

"Local .vimrc for the user
" 1. get the user, which used su
" 2. we can load his .vimrc.<user>
" from $HOME (from where we have sudo'ed in)
let b:term = substitute( system ("ps T | grep ' ps T$' | sed -e 's/^  *//' | cut -d ' ' -f 2 "), "\n", "", "" )
let b:user = substitute( system ("who | grep ".b:term." | cut -d ' ' -f 1 "), "\n", "", "" )
let b:file = $HOME."/.vimrc.".b:user
if filereadable(b:file)
  execute 'source '.b:file
endif

Upvotes: 0

mpeaton
mpeaton

Reputation: 111

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.

Upvotes: 7

turningtaxis
turningtaxis

Reputation: 21

I'm assuming that your initial owner owns your tty. If so, you can get your initial USER with:

stat -c'%U' `tty`

By placing your customized root .vimrc in /root/.vimrc.$USRNAME you can keep a reasonably secure customized vimrc file. You can do other things too, but I leave that to your imagination.

Method 1 - put this in your /root/.bashrc & smoke it:

# Source a custome vimrc if it exists
mytty=`tty`
initial_user=`stat -c'%U' $mytty`
custom_vimrc="/root/.vimrc.$initial_user"
if [ -f $custom_vimrc ]; then
    export VIMINIT="source $custom_vimrc"
fi

Method 2 - put something similar in your /root/.vimrc (a better solution since you might use ksh).

If anyone can figure out Method 2, I'd welcome the post. I lack motivation.

Upvotes: 2

frabjous
frabjous

Reputation: 1037

Personally, I just symlink Root's .vimrc's to mine. From BASH (as root):

ln -s /home/<me>/.vimrc /root/.vimrc

But you do need to be careful about what's in it.

Upvotes: 1

Randy Morris
Randy Morris

Reputation: 40927

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.

Upvotes: 4

Alison R.
Alison R.

Reputation: 4294

In vim:

:source /path/to/your/.vimrc

Upvotes: 6

Related Questions