Showing posts with label OS X. Show all posts
Showing posts with label OS X. Show all posts

Thursday, April 8, 2010

How to change your OSX terminal color for ssh sessions

Sometimes I have lots of terminal windows open for ssh sessions to multiple machines. It can all be a bit confusing even if I use the remote machine name in the shell prompt. So I got terminal colorization working for me with help from here and here.
There is a default color for all screens that are using ssh and you can also customize the colors for different remote machines. When you end the ssh session, the terminal returns to black on white.

This is for OS X because it uses the osascript command to change the colors but the rest of the shell script should be just fine for Unix.


$ cat /usr/local/bin/ssh
#!/bin/sh

# Color the OSX Terminal app according the hostname used in ssh
# Assumes you log in with a simple "ssh myuserid@remotehost"
# Place in a directory such as /usr/local/bin that is before the real ssh location

# The value of whatever was after the @ in the command
HOSTNAME=`echo $@ | sed s/.*@//`

set_bg () {
osascript -e "tell application \"Terminal\" to set background color of window 1 to $1"
}

set_text () {
osascript -e "tell application \"Terminal\" to set normal text color of window 1 to $1"
}

# Return to black text on white background
on_exit () {
# echo exited
set_bg "{65535,65535,65535}"
set_text "{0, 0, 0}"
}
trap on_exit EXIT

# My color names
# green 10000,40000,40000
# blue 10000,20000,60000
# orange 60000,40000,00000
# red 60000,00000,00000
# yellow 60000,60000,40000

case $HOSTNAME in
fisheye) set_bg "{60000,60000,40000}" ; set_text "{0,0,0}" ;;
jira|jira2) set_bg "{10000,40000,40000}" ; set_text "{0,0,0}" ;;
# unused colors follow
# blue) set_bg "{10000,20000,60000}" ; set_text "{65535,65535,65535}" ;;
# orange) set_bg "{60000,40000,00000}" ; set_text "{0,0,0}" ;;
# red) set_bg "{60000,00000,00000}" ; set_text "{0,0,0}" ;;
# yellow) set_bg "{60000,60000,40000}" ; set_text "{0,0,0}" ;;
# gray) set_bg "{60000,57000,55000}" ; set_text "{0,0,0}" ;;
*) set_bg "{60000,57000,55000}" ; set_text "{0,0,0}" ;;
esac

/usr/bin/ssh "$@"

Wednesday, May 14, 2008

Emacs, OS X and the EDITOR variable

This took a bit of fiddling to get right, so I offer it up to the Great Google Index with the hope that it may save someone else some time. The EDITOR environment variable is what is used by Unix applications (such as Subversion and git) when they want to start an editor for input. Starting emacs from the command line in OS X uses the open command.

To get this working with emacs and OS X Leopard I had to make the file /usr/bin/emacs contain:

#!/bin/sh
open -nWa /Applications/Emacs.app/Contents/MacOS/Emacs "$@"

This opens a file using a new instance of emacs and waits until emacs exits.