Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

All of the approaches that I have seen for changing the background color in Terminal use applescript; is there an escape sequence? All of the hints that I've seen, like this one, use AppleScript. I want to put it in my .base_profile and in the 'ssh' command so that when I hop from system to system the color changes properly. (I sometimes have to hop multiple systems in due to connectivity issues)

share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

I have this in my bash_profile:

# Change the terminal theme when SSH to remote
function tabc() {
    NAME=$1; if [ -z "$NAME" ]; then NAME="Default"; fi
    osascript -e "tell application \"Terminal\" to set current settings of front window to settings set \"$NAME\""
}

# Change the terminal color when remote
function ssh {
  tabc "Hiltmon-Remote"
  /usr/bin/ssh "$@"
  tabc "Hiltmon"
}

It uses Applescript (which you don't want) to change the terminal profile. Replace the Hiltmon and Hiltmon-Remote with your own color scheme names. When you use the ssh command, the macro changes the shell theme, then runs ssh. When you exit the remote session, it changes it back. Very cool!

I tried using the shell color escape sequences like \[\e[36m\] and \[\e[0m\] but the remote session can and does reset them. This works great for me.

share|improve this answer
    
This doesn't work for me because I am using SSH from A->B and then from B->C. The shell running on C needs a way to change the color of the Terminal running on A. –  vy32 Sep 14 '12 at 21:19
    
Create a ssh2 command in the profile to set a different terminal theme when you ssh from a->b->c. So you go ssh2 b and then ssh c. Shells on remote machines cannot change the theme on the local machine as far as I know. –  Hiltmon Sep 15 '12 at 2:13
    
That's a bit awkward. I recently found that there is an escape sequence that will change the background of an xterm but not GNOME Terminal. See superuser.com/questions/592090/…. SO there is probably a way to do it with Terminal.app. If only I had a list of all the escape sequences that Terminal supports. –  vy32 May 9 '13 at 12:32
add comment

Use something like

tput setab <n>

See background coloring for values to use for n. You may want to follow this by the clear command to repaint the whole window. Note that this will not persist if you embed escape codes in your prompt that reset colors or if you do something else that resets terminal colors.

share|improve this answer
add comment

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.