Set terminal title to current directory

May 02, '02 08:42:23AM

Contributed by: solaris

Want to display your hostname and current directory in Terminal.app's title bar? Now you can!

  1. Create a script called chg_dir.sh (/Library/Scripts is a good place) with contents as follows:
    #!/bin/sh
    dir=$1
    if [ ! ${dir} ] ; then
    dir="$HOME"
    fi
    echo -n "^[]0;`uname -n`:${dir}^G"
    NOTE: ^[ represents the escape key, ^G represent control-G. To type these non-printing characters in vi, use ctrl-V and then "escape" and "control-G".

  2. Edit your .cshrc (or equivilant) and add these two lines:
    echo -n "^[]0;`uname -n`:${HOME}^G"
    alias cd "cd !* ; /Library/Scripts/chg_dir.sh !*"
Now when you start a new terminal session, the title is set to "host:/home/dir". When you change dir it becomes host:/current/dir".

[Editor's note: There was another way to accomplish this same result posted in an earlier hint, but this one was unique enough to merit its own mention. I tested this, and it works as described. I added the two lines to my existing "aliases.mine" file, and placed the script in ~/bin. I still personally prefer this hint, which explains how to show the current command in the title bar, but the directory info is also useful.]

Comments (4)


Mac OS X Hints
http://hints.macworld.com/article.php?story=2002050208422313