Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Display latest jobs in Terminal titles (revisited) UNIX
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:
  • In bash 2.05b or newer(default since Panther):
     trap 'printf "\033]0;  `history 1 | cut -b8-`  \007"' DEBUG
  • in ksh ksh93 and newer:
     trap 'printf "\033]0;  `history -0 | cut -b5-`  \007"' DEBUG
  • in tcsh 6.08.03 and newer (default before Panther):
     alias postcmd 'printf "\033]0; `history -h 1` \007"'
  • in zsh 3.0.6 or newer:
     preexec() { printf "\033]0;  `history $HISTCMD | cut -b7-`  \007"; }
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.]
    •    
  • Currently 1.25 / 5
  You rated: 2 / 5 (4 votes cast)
 
[14,494 views]  

Display latest jobs in Terminal titles (revisited) | 6 comments | Create New Account
Click here to return to the 'Display latest jobs in Terminal titles (revisited)' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
This hint is busted
Authored by: SOX on Jul 02, '07 08:30:06AM

What's with the 33]0 ?
this just shows up in my prompt as literally 33]0. is there some escape missing here in your post.
also the hint blows because the command line becomes too long. should be truncated. Even better put this into the windows title bar.



[ Reply to This | # ]
This hint is busted
Authored by: hysterion on Jul 02, '07 08:39:12AM
robg emailed me that he he was fixing it, but as of right now only 1 of 9 missing backslashes (and zeros) are restored. Every 33 and 07 in the code should really be B033 and B007, where B stands for backslash (as they are in the second "Further remark").

In comments, html-entitly encoded backslashes (ampersand+pound sign+9+2+semicolon) may work if restored after each "preview" cycle--here's a try:

"Every 33 and 07 in the code should really be \033 and \007."

Your other point on overlong lines is addressed in my third "Further remark", I think...

[ Reply to This | # ]

Display latest jobs in Terminal titles (revisited)
Authored by: foon on Jul 02, '07 10:08:53AM

This is cool. Is there a simple way to have this happen only on a local terminal? That is, I often ssh to other machines, and in that case I'd like the "ssh other.machine.com" to remain the title, but if I'm working locally I want the normal behavior. I'd hope some sort of tty juju could figure this out...



[ Reply to This | # ]
Display latest jobs in Terminal titles (revisited)
Authored by: foon on Jul 02, '07 10:33:24AM
if [[ ! `printenv SSH_CONNECTION` ]]; then trap 'printf "33]0; `history 1 | cut -b8-` 07"' DEBUG; fi

[ Reply to This | # ]
Display latest jobs in Terminal titles (revisited)
Authored by: wgscott on Jul 02, '07 01:17:53PM
In zsh, I like to display by default the current working directory along with the penultimate directory in iTerm's tab. I also like it to display the active program only while it is running (eg vim, man), and then automatically return to the default tab label. I've been using this pair of functions to do so:

function preexec { printf "\e]1; $(history $HISTCMD | cut -b7- ) \a" }
function precmd { printf "\e]1;$PWD:h:t/$PWD:t\a" }

Note that
\e]1
puts the label in the tab only (or the xterm icon label),
\e]2
in the title only, and
\e]0
in both. I also use the
chpwd
function to set both the title bar and tab to new values when the PWD is changed (cd is executed), i.e.,

function chpwd { printf "\e]2;[zsh] $HOST:r:r::$PWD\a" ; printf "\e]1;$PWD:h:t/$PWD:t\a" }

[ Reply to This | # ]
Display latest jobs in Terminal titles (revisited)
Authored by: etresoft on Jul 02, '07 06:24:20PM
By some odd coicidence, I did this very thing today only minutes before I saw the posting here.
I found a solution here. It works with bash in both MacoS X 10.4 Terminal and PuTTY on Windows.

[ Reply to This | # ]