Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is there a way to read a .vimrc file for only a single ssh session? That is, when I log in I perform some operation so that vim uses say /tmp/myvimrc until I log out?

I do not want to permanently overwrite the current .vimrc, I just need to use a different set of settings for the duration of my login every once in a while.

share|improve this question
add comment

3 Answers

up vote 8 down vote accepted

Suppose you have this other set of settings in /tmp/myvimrc. If my reading of man vim is correct you can start vim with this set of settings using the following:

$ vim -u /tmp/myvimrc

Thus, to make this an option for the rest of the session, I would create a function that sets this as an alias for vim. Thus, in bash I would put something like this in my .bashrc file:

function vimswitch {
    alias vim='vim -u /tmp/myvimrc'
}

Then, when I wanted my new vim settings, I would just run:

$ vimswitch

Note that I wouldn't store myvimrc in /tmp since this could easily be cleared out upon reboot. If you are using a shell other than bash this should still be possible, but the syntax could differ slightly.

share|improve this answer
1  
nice! That's what I call going the whole 9 yards. –  Robert Gowland Nov 18 '10 at 18:07
add comment

You can use the VIMINIT environment variable to override the use of the usual .vimrc while keeping other parts of the initialization process. VIMINIT should be set to one or more ex-style commands (“colon” commands; use pipe (|) to separate multiple commands), not just the path to a different initialization file.

VIMINIT='so /tmp/myvimrc'; export VIMINIT

vim whatever # uses /tmp/myvimrc, not ~/.vimrc

The main difference from using -u is that VIMINIT will still allow the other parts of initialization process to be used (e.g. system vimrc, evim.vim (if applicable), et cetera).

share|improve this answer
2  
To automatically setting VIMINIT variable while logging via ssh you could use file ~/.ssh/environment. This file could be used only for setting environment variables, so only entry you should add is: VIMINIT='so /tmp/myvimrc'. This file is processed only when in sshd configuration option PermitUserEnvironment is set to yes, so you should test on your own if it works for you or ask your administrator if it is turn on. –  pbm Nov 18 '10 at 21:46
add comment

When you log in via ssh, ssh sets variable $SSH_CONNECTION. Your .bashrc could check for this var and if it is set sets alias that you want:

if [ -n "$SSH_CONNECTION" ]
then
        alias vim='vim -u /tmp/myvimrc'
fi
share|improve this answer
    
+1 for SSH_CONNECTION tip –  anishsane Feb 26 '13 at 12:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.