English 中文(简体)
What s an easy way to have terminal use a different color based on ssh host name?
原标题:

Using putty in windows, you can save sessions that connect to a certain host and use a certain text color...this was super useful for me since I work with a bunch of remote hosts and I m wondering if there is (there must be) a way to get Terminal (in Snow Leopard) to emulate this behavior.

I m wondering how I would 1. Save a connection (e.g. username@hostname.com) and have that connection always open with a certain text color (e.g. #00ff00) 2. Ideally, have any terminal window detect what host it was in and change its color accordingly. So if I was in my regular Terminal environment and issued a successful ssh username@hostname.com, it would automatically change the text color of that terminal window (or tab) to #00ff00

Let me know, thanks!

最佳回答

In terminal you can define profiles with different window background color, opacity, etc. Also in profiles you can specify a startup command. You could set up a different profile for each host you use with a startup command of "ssh me@thathost", but this would only work for new windows. Profiles are easy to get to via Shell -> New Window.

问题回答

OK, if you insist on invoking ssh from the command line, here s something that should do the trick: write a shell script and save it somewhere as colorssh.sh. When it runs, it looks at its arguments for a matching host and sets the active terminal window s colors appropriately. Then it invokes the real ssh, passing along those arguments. When ssh returns execution to the script, it sets the colors back to normal.

Since you probably want to keep typing ssh instead of colorssh.sh, you can set an alias in your .profile.

As for the script itself? Here is teh codez:

#!/bin/bash

function setTerminalColors {
    osascript 
        -e "tell application "Terminal"" 
        -e "tell selected tab of front window" 
        -e "set normal text color to $1" 
        -e "set background color to $2" 
        -e "end tell" 
        -e "end tell"
}

for ARG in $*
do
    case "$ARG" in
        host.example.com)
        username@host.example.com)
            setTerminalColors "{0,65535,65535}" "{65535,0,0}"
            ;;
        username@otherhost.example.com)
            setTerminalColors "{65535,65535,0}" "{0,65535,0}"
            ;;
    esac
done

ssh $*

# back to normal
setTerminalColors "{0,0,0}" "{65535,65535,65535}" 

You ll have to edit the script to add new host/color combinations.

Note that colors must be specified as an RGB triplet of integers in the range 0-65535. I know, weird, right?

Technically, the AppleScript portion changing deprecated properties. You re supposed to change a window s colors via its "settings set" property, but I suspect that would change all windows using that settings set, not just the current one.

Also, this script assumes that black on white is your "normal" setting. If that s not the case you could change the script to save the current values before running or use the colors from the default settings set.





相关问题
2 mysql instances in MAC

i recently switched to mac. first and foremost i installed xampp. then for django-python-mysql connectivity, i "somehow" ended up installing a seperate MySQL. now the seperate mysql installation is ...

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Controlling OSX windows

I m trying to control windows of a foreign OSX applications from my application. I d like to 1. move the windows on the screen 2. resize the windows on the screen 3. change the currently active window ...

Switching J2SE versions on Mac OS (SnowLeopard)

My current JDK on Mac OS (10.6) is set to 1.6 and I d like to switch to 1.5. A listing of /System/Library/Frameworks/JavaVM.framework/Versions/ shows: lrwxr-xr-x 1 root wheel 10 Nov 3 18:34 ...

Scrolling inside Vim in Mac s Terminal

I ve been googling around trying to figure out if it s possible to use my mouse wheel to scroll while inside Vim in Mac s Terminal, with no luck. It seems as if only X11 or iTerm support this. Before ...

export to MP3 from quicktime API

A question for Apple,QT programmers. Would like to know if it s possible to export a Movie object to MP3 using the QuickTime API. Preferably the ConvertMovieToFile function. I ve looked at ...

热门标签