Ssh – vim: Use different colorscheme when connected over SSH

colorssshvim

On my desktop I use the hbrid color scheme in vim, with the preferred colors set for my consoles 16-color palette (as described in the README).

However, on my laptop, I use the solarized color scheme, so when I SSH into my desktop from my laptop and open VIM it will look funny using the hybrid colors.

Is there any way I can 'forward' a vim configuration to set the colors when SSHing into a different machine? (Assume the colorscheme file is already installed, and all that needs to be done is call colorscheme solarized)

Best Answer

You can access environment variables in your vimrc using $NAME. ssh sets the environment variable SSH_CONNECTION within an SSH session to non-empty metadata about the connection. You can combine these two to run configuration code based on whether you're accessing vim over SSH or not:

if $SSH_CONNECTION
    colorscheme solarized
endif

The body of the if runs when SSH_CONNECTION is non-empty, so if you put this into your desktop's .vimrc then you'll get the solarized theme over SSH, and whatever you configured earlier the rest of the time.

Related Question