96 Set the Terminal Title Dynamically


#96 Set the Terminal Title Dynamically

This is a fun little script for Mac OS X users who like to work in the Terminal application. Instead of having to use the Terminal > Window Settings > Window dialog box to set or change the window title, you can use this script to change it whenever you like.

The Code

 #! /bin/sh # titleterm - Tells the Mac OS X Terminal application to change its title #   to the value specified as an argument to this succinct script. if [ $# -eq 0 ]; then   echo "Usage: 
 #! /bin/sh # titleterm - Tells the Mac OS X Terminal application to change its title # to the value specified as an argument to this succinct script. if [ $# -eq 0 ]; then echo "Usage: $0 title" >&2 exit 1 else echo -ne "\033]0;$1\007" fi exit 0 
title" >&2 exit 1 else echo -ne "3]0;
 #! /bin/sh # titleterm - Tells the Mac OS X Terminal application to change its title # to the value specified as an argument to this succinct script. if [ $# -eq 0 ]; then echo "Usage: $0 title" >&2 exit 1 else echo -ne "\033]0;$1\007" fi exit 0 
7" fi exit 0

How It Works

The Terminal application has a variety of different secret escape codes that it understands, and the titleterm script sends a sequence of ESC ] 0; title BEL , which changes the title to the specified value.

Running the Script

To change the title of the Terminal window, simply type in the new title you desire .

The Results

There's no apparent output from the command:

  $ titleterm $(pwd)  

However, it instantly changes the title of the Terminal window to the present working directory.

Hacking the Script

With one small addition to your . cshrc or .bashrc (depending on what login shell you have), you can automatically have the Terminal window title always show the current working directory. To use this to show your current working directory, for example, you can use either of the following:

 alias precmd 'titleterm "$PWD"'                            [tcsh] export PROMPT_COMMAND="titleterm \"$PWD\""                [bash] 

If you run either the tcsh shell (the default login shell for 10.2. x ) or the bash shell (the default shell for 10.3. x , the so-called Panther release of Mac OS X), you can drop one of the commands above into your .cshrc or .bashrc , and, starting the next time you open up a Terminal window, you'll find that your window title changes each time you move into a new directory!




Wicked Cool Shell Scripts. 101 Scripts for Linux, Mac OS X, and Unix Systems
Wicked Cool Shell Scripts
ISBN: 1593270127
EAN: 2147483647
Year: 2004
Pages: 150
Authors: Dave Taylor

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net