user3534472
user3534472

Reputation: 341

Change oh-my-zsh theme when ssh is run

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.

Upvotes: 9

Views: 16262

Answers (2)

Magus
Magus

Reputation: 2982

You could prepend the host name to your prompt.

Following this gist

Just paste the contents of this file on:

~/.oh-my-zsh/themes/robbyrussell.zsh-theme

Then basically on your local machine:

enter image description here

And on you remote machine:

enter image description here

In my case, ded12 is the hostname of my remote and I only changed the theme files on my remote, if I changed it locally it would have my local hostname also prepending the prompt

Upvotes: 6

Paolo Raez
Paolo Raez

Reputation: 1753

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

Upvotes: 18

Related Questions