Display latest jobs in Terminal titles (revisited)

Jul 02, '07 07:30:00AM

Contributed by: hysterion

Showing the latest job in each window title is my preferred way to tell multiple Terminals apart. Unfortunately the GUI method is rather useless (File » Set Title... » Active Process Name will make most windows say bash), and this old hint only covered tcsh. To update it, here is (in a nutshell) the command to issue, or to put in the appropriate startup file:

While the how-to cited in the old hint did contain the zsh and tcsh methods, and while in retrospect the ksh trap '...' DEBUG method had been working since long before, the idea of using it for the present purpose seems newer and due to Paul Jarc, who first mentioned it shortly after bash switched to the ksh93 standard of running '...' before (rather than after) each interactive command. (Prior to that, the effect was reputedly impossible to obtain without recompiling bash.)

Lately zsh (≥ 4.3.3) also rescheduled the trap, so recent versions also support Jarc's method -- provided you preface it with set -o debugbeforecmd.

Further remarks and variants:

  1. In principle, we could replace the whole `history ...` part by the "latest command" variable maintained by each shell: $BASH_COMMAND in bash (≥ 3.2), ${.sh.command} in ksh, \!# in tcsh, and $1 in zsh. In practice, however, tcsh's version is buggy and the bash/ksh versions will only show the last segment of pipeline commands, so this really only works well in zsh.
  2. For the mechanism to keep working across hosts over ssh, you need to set it up in shell startup files on the remote machine, and perhaps unset there any variables that write to the title bar -- typically PROMPT_COMMAND on Red Hat machines. (Scan the output of set for anything involving \033...\007, and for anything else you may want to add to the title bar -- e.g. I like it to start with as a reminder of where I am.)
  3. If a command is too long to fit in the title bar, then (unlike xterm) Terminal.app will truncate it from the left. To truncate from the right and still use the whole available width, I complete the cut part above as follows (in ksh, say): cut -b5-$(( ${COLUMNS:-80} - 20 )).
  4. [robg adds: I haven't tested this one.]

    Comments (7)


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