Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'd like to have my terminal background color change based on hostname. My reasoning for this is mainly to have and extra way to remind myself what server I am on, so I don't do something stupid.

Is there someway I can have it use the standard aubergine background if I am on my local machine, then change red if I ssh into a production web server, or other hosts that I could specify?

share|improve this question
    
One idea, script the change to the environmental variable that handles the terminal settings. Make a script for each place you typically ssh to and include the change to the settings in that script, here is a guide that can help: cyberciti.biz/faq/… - this way you can just run a script that will not only connect for you, but will customize the terminal for that particular session. –  MaQleod Oct 17 '11 at 17:31
1  
There's the same question on the ubuntu site: askubuntu.com/questions/13705/… Mind, I don't blame you, I did not find it at first either :) –  Stefano Oct 17 '11 at 17:31

3 Answers 3

It does not seem that there is any functionality in gnome-terminal to add a new tab to an existing window from the command-line. But there are a few options to accomplish what you want.

Per Command Profiles

Create a new gnome-terminal profile for each host you will SSH into. If you only have a few hosts that you regularly connect to, this might be the simplest. Each profile can have a different title, foreground color, background color, custom command and other settings defined. Then you can use File -> Open Tab to open a new tab with the selected profile.

Reuseable Profile

Create a new gnome-terminal profile that will be used to open a new window each time you want to connect to a different SSH host (based on this AskUbuntu answer that Stefano pointed out). This would work good if you connect to many different hosts frequently. This will not allow you to distinguish between different gnome-terminal windows where you are connected to different hosts solely on the background/foreground colors, but you will have a different title per window.

  1. Create a new gnome-terminal profile (File -> New Profile) based on the Default profile and call it "RemoteHost" (note, no spaces in "RemoteHost" to make commands easier).
  2. Under the Title and Command tab, change:
    1. Initial title: to "Remote Host"
    2. When terminal commands set their own titles: to Replace initial title
  3. Under the Colors tab, change:
    1. Uncheck Use colors from system theme
    2. Build-in schemes: to Custom
    3. Text color: and Background color: to colors of your choosing. Keep in mind that some commands (like ls) use colors for their output and you don't want to pick colors that will make it difficult to read the output.
  4. Click on the Close button to save your new profile.
  5. Now you can open a new gnome-terminal window for each remote SSH host using the command gnome-terminal --window-with-profile=RemoteHost -t "Some Remote SSH Host" -x ssh user@somehost. The -t option sets the gnome-terminal window title and the -x option executes the rest of the command line in the terminal. You could even make an alias to shorten total keystrokes.

Command-Line

I found this blog entry with the following script that uses the xdotool and wmctrl commands (they weren't installed by default on Ubuntu, so you might need to install them first) to use the gnome-terminal Ctrl + Shift + t keyboard shortcut to open a new tab in the current gnome-terminal window. It could be modified to open a new tab with a specific profile and execute some command for you.

#!/bin/bash
# Path: /usr/local/bin/gnome-terminal
if [ "x$*" != "x" ]; then
  /usr/bin/gnome-terminal "$@"
else
  pgrep -u "$USER" gnome-terminal | grep -qv "$$"
  if [ "$?" == "0" ]; then
    WID=`xdotool search --class "gnome-terminal" | head -1`
    xdotool windowfocus $WID
    xdotool key ctrl+shift+t
    wmctrl -i -a $WID
  else
    /usr/bin/gnome-terminal
  fi
fi

Other

You could get creative and try some other things.

This SuperUser answer basically uses a bit of "script-fu" acrobats to create a temporary gnome-terminal profile that is used to open a new window. It may be modified for your use.

You could probably use this StackOverflow Q&A and more "script-fu" acrobats to dynamically change the gnome-terminal title whenever you SSH to a remote host. It would not be as prominent as background/foreground color changes, but it would be better than a standard Terminal title all the time.

share|improve this answer

This will work in Gnome, IF you're willing to use a new gnome-terminal window for each ssh session.

1) Create a new profile (with different background colour) called "Remote"

2) Insert the following into .bash_aliases, or .bashrc

### add to .bash_aliases, for differentiating between local and remote hosts 
sshhelper() {
    gnome-terminal --window-with-profile=Remote -x bash -c "ssh $1";
}
alias sshc=sshhelper

Now sshc remote-machine opens a new gnome-terminal session with the "Remote" profile. this will differentiate between local and remote profiles.

To accommodate multiple profiles/hosts, create multiple profiles and place something like this in .bash_aliases instead :

### add to .bash_aliases, for differentiating between multiple remote hosts
sshhelper() {
    HOST=`echo $1 | cut -d'@' -f2`

    case $HOST in
        Production )  PROFILE="Red" ;;
        Test )        PROFILE="Green" ;;
        # ... if you have more cases ...
        *)           PROFILE="Default" ;;
    esac

    gnome-terminal --window-with-profile=$PROFILE -x bash -c "ssh $1";
}

# alias ssh=sshhelper    # this will "override" the ssh command, but may break other stuff!
alias sshc=sshhelper

Now sshc Production will open a new session window using the "Red" profile, sshc Test will open a new session window using the "Green" profile, and other hosts will use the "Default" Profile.

share|improve this answer

You can use ssh/config's localcommand option to run a command whenever an alias is used. I use

   host hostname
       user myusername
       localcommand xtermcontrol --bg '#abc'

This depends on xtermcontrol and your term being xterm. Presumably there are other apps for other terms.

The only problem with this approach is that it happens when you call ssh. There's nothing to undo the color change. I've done it by wrapping a function around ssh, but that has its drawbacks too.

  function ssh() {
    FG=$(xtermcontrol --get-fg)
    BG=$(xtermcontrol --get-bg)
    $(which ssh) "$@"
    xtermcontrol --fg="$FG"
    xtermcontrol --bg="$BG"
  }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.