## 4.8. Session Info in Window Title or Status Line

Some people don't like to put the current directory, hostname, etc. into their prompts because it makes the screen look cluttered to them. Here's another idea. If your terminal or window system has a status line or titlebar, you might be able to put the information there. That's nice because you can see the information while you run programs. The down side is that the information can get out of date if you use a command that takes you to another host or directory without updating the status line. The latest bash and zsh shells do this by default when you're using an xterm window. For the rest of you, here's how to do it yourself. Because neither csh or tcsh do this by default, I'll show C-shell-type syntax. But you can do the same thing in Bourne-type shells with a shell function and case (Section 35.10) statement; there's a ready-to-use version on the web site.

When you use cd, pushd, or popd, an alias uses the echo command to write special escape sequences to the terminal or window.

Here are cd aliases and other commands for your .cshrc or .tcshrc file. If I were logged in to www.jpeek.com in the directory /home/jpeek, this alias would put:

>     www:/home/jpeek

in the status area or window title, depending on which terminal type I'm using. Of course, you can change the format of the status line. Change the following command string, `${host:h}:${cwd}`, to do what you need; see your shell's manual page for a list of variables, or create your own custom information.

`:h` Section 28.5, `&&` Section 35.14

Go to http://examples.oreilly.com/upt3 for more information on: stattitle.cshstattitle.sh

>     set e=`echo x | tr x '\033'`   # Make an ESCape character

>     set g=`echo x | tr x '\07'`    # And a Ctrl-g
>     set host=`uname -n`
>     # Puts $host and $cwd in VT102 status line. Escape sequences are:
>     # ${e}7 = save cursor position, ${e}[25;1f = go to start of status
>     # line (line 25), ${e}[0K = erase line, ${e}8 = restore cursor
>     alias setstatline 'echo -n "${e}7${e}[25;1f${e}[0K    ${host:h}:${cwd}${e}8"'
>     alias settitle 'echo -n "${e}]2;${host:h}:${cwd}${g}"'
>     switch ($TERM)
>     case vt10?:
>       alias cd 'cd \!* && setstatline'
>       alias pushd 'pushd \!* && setstatline'
>       alias popd 'popd \!* && setstatline'
>       breaksw
>     case xterm*:
>       alias cd 'cd \!* && settitle'
>       alias pushd 'pushd \!* && settitle'
>       alias popd 'popd \!* && settitle'
>       breaksw
>     endsw

(Section 5.15 has more about how this works in xterms.)

The ESC and CTRL-g characters are stored with a trick that should work on all Unix shells. Most modern echos will let you make a nonprintable character directly, like this: `g='echo '\07''`.

If you always use a VT102-type terminal (and many people do), the setstatline alias will work fine. If you use a different terminal, try it anyway! Otherwise, read the terminal manual or its termcap/terminfo entry and find the escape sequences that work for it; then add a new case to the switch statement.

Note that you might have some trouble here: if this code is in your .cshrc file but your terminal type is set in your .login file, the terminal type may not be set until after the alias has been read. There are workarounds (Section 3.8).

The status line or titlebar can also get out of sync with reality if you use remote logins (Section 1.21), subshells (Section 24.4), etc. These might make a new status line or titlebar but not reset the original one when needed. To fix this, just type setstatline or settitle at a shell prompt. Or, if you don't want to bother to think of the name of the alias, use the following command to change to the current directory (.), which will use the correct alias and reset the status or title:

>     % cd .

If you're using tcsh, its special alias cwdcmd will be run every time you change the shell's current directory. So, in tcsh, you can replace the three aliases for cd, pushd, and popd with something like this:

>     alias cwdcmd settitle

--JP and SJC

Copyright © 2003 O'Reilly & Associates. All rights reserved.