Change oh-my-zsh theme when ssh is run

11,660

Solution 1

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

Solution 2

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

Share:
11,660
user3534472
Author by

user3534472

Updated on July 23, 2022

Comments

  • user3534472
    user3534472 almost 2 years

    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.