Temporary .vimrc

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.

Asked By: Robert Gowland


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.

Answered By: Steven D

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" ]
        alias vim='vim -u /tmp/myvimrc'
Answered By: pbm

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).

Answered By: Chris Johnsen
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.