Jul 02, '07 07:30:00AM • Contributed by: hysterion
- 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"; }
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:
- 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.
- 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.)
- 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 )). [robg adds: I haven't tested this one.]
