tcsh and xterm tricks

Edward Glowacki glowack2@msu.edu
Fri, 21 Sep 2001 16:24:00 -0400


TCSH/Xterm Hints
================

Well, I just finished tweaking some interesting tcsh/xterm hacks
that will hopefully make life easier now.  My xterms used to just
have the hostname as their title, which is going to be the same
for any and all terms unless I explicitely set them to something
else.  Finding the one you want can sometimes be a challenge...
Now they each have a title of the form:

user @ hostname: /current/path/ > last_command

which is almost identical to my command prompt (except the extra
spaces for readability and the last command being displayed).
Basically what this helps with is choosing terminals from a list
of windows because it gives you some reference context for the
window.  This works really well with the way the Gnome taskbar
handles windows.  Right now I have Gnome set up to stack like
windows together, so if I have 3 xterms open, it would show up in
the task bar as "(3) xterm" (as opposed to "xterm" "xterm" "xterm"
as 3 separate items).  Click on it, and it pops up a list of all
the titles of the different xterms, in my case telling me I'm
running "vim .cshrc", "/home/glowack2> ls" and "/usr/ports/www/mozilla>
make" (building the lizard! ;) ).  Another really good thing they
did right is when a window is active, the task bar highlights it
and changes the text it's showing from "(3) xterm" to "vim .cshrc"
(it's a small thing, but really is a good usability feature.  Now,
if they'd only make the menus act more like those in QT[1], I'd
be a happy camper!).

Anyways, the way I do the xterm titles is with escape sequences (I
found an example on the web somewhere and modified from there).
In tcsh, you can set things to run before every command, so I used
that feature to spew out the escape sequence and set the window
title bar.  Here's a snippet from my .cshrc (with some line numbers)
showing how it's done:

1 :if($?DISPLAY) then
2 :  switch ($TERM)
3 :    case "xterm*":
4 :      alias cwdcmd '/bin/echo -n "^[]0;${USER} @ ${HOSTNAME}: $cwd >^G"'
5 :      alias postcmd '/bin/echo -n "^[]0;${USER} @ ${HOSTNAME}: $cwd > \!#^G"'
6 :      breaksw
7 :  endsw
8 :endif

Lines 1,8 set this so it's only run if we're in an X environment.
Lines 2,7 are a switch, so you could add a different set of commands
    for say gnome-terminal or konsole
Line  3   matches all terminal types that start with xterm (i.e.
    xterm, xterm-color are the main ones)
Line  4   sets the command to be run every time you change directories (cd)
            this is there for consistency and to keep the title up-to-date
Line  5   sets the command to be run every time you run a command

bash or other shell users will probably have to change lines 4 and
5, and even the stuff it echos since I'm using csh style variables
there.

Line 5 in more detail:

5 : alias postcmd '/bin/echo -n "^[]0;${USER} @ ${HOSTNAME}: $cwd > \!#^G"'

^[ is the escape character.  In vim, you can get it with "control-v escape"
]0; is the xterm escape sequence to set the window title AND icon name
${USER} is the variable USER, which should be set by the shell
${HOSTNAME} is the variable HOSTNAME, which I set earlier in my
  .cshrc with the command "set HOSTNAME=`hostname -s`".  DO NOT PUT
  THIS IN WITHOUT CHECKING THE hostname MAN PAGE!!!  On some systems
  (Solaris in particular), "hostname -s" will SET your hostname to
  "-s", which is a Bad Thing(TM).  You have been warned. =)
$cwd is the current directory, supplied by the shell
\!# is the command you ran.
^G is the BEL character.  In vim, you can get it with "control-v control-g"

Anything between the "^[]0;" and the "^G" will be the title, so
feel free to play with variables to change the prompt to suit you.

If these aliases cause trouble, you can get rid of them with "unalias
postcmd" and "unalias cwdcmd", and as a note, you should probably
put those two unalias commands in the beginning of your .cshrc,
I've had extreme difficulty with it otherwise, specifically with
"source .cshrc" to reload your stuff, it tends to gak big-time!

These escape sequences also work with Eterm and gnome-terminal.
They work with Konsole too, except that Konsole adds " - Konsole"
on the end of the title. =P


[1] QT menus and sub-menus work a LOT nicer than GTK menus.  I'll
post my explanation in a bit... =)

-- 
Edward Glowacki				glowack2@msu.edu
GLLUG Peon  				http://www.gllug.org
Imagination is the one weapon in the war against reality.
                -- Jules de Gaultier