Change oh-my-zsh theme when ssh is run

user3534472 picture user3534472 · Jul 10, 2014 · Viewed 9.8k times · Source

I have Oh-my-zsh installed on iterm2. How can I make my terminal change theme whenever ssh is run? It would be nice if the script also changes the background to one of the presets imported.

I am a complete bash noob. Please explain in newbie language.

Answer

Paolo Raez picture Paolo Raez · Jun 22, 2017

The first thing you need to know is that .zshrc is a script that is run just before you open a new session on zsh. So, providing that zsh is your default shell, when you open a terminal on your local machine, it runs .zshrc. When you open a ssh session to that machine, it runs .zshrc too!

Inside .zshrc you can find that piece of code commented:

# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
#   export EDITOR='vim'
# else
#   export EDITOR='mvim'
# fi

You can use this example to achieve your goal, just this way:

if [[ -n $SSH_CONNECTION ]]; then
  ZSH_THEME="robbyrussell"
else
  ZSH_THEME="agnoster"
fi

Where robbyrussell will be used on your ssh sessions, and agnoster for the rest. So just substitute the line where you set your theme for the if/else statement above, and customize it