On Fri, 21 Nov 2008, Mauricio wrote:
> Hi,
>
> Is it possible to change mlterm window title according to the
> application in its foreground?
Hi Maurício,
It's certainly possible to change the title. To do that, you need to
print to the terminal:
\033]0;whatever title you want\007
Where the special characters are:
\033 = 0x1b = ^[ = \e = ANSI escape.
\007 = 0x07 = ^G = BELL.
(Different escapes depending on how you're printing)
This usually depends on your shell. I use Zsh, which isn't the most common
shell (though I highly, highly recommend it -- it's a very easy transition
from Bash). With it, I accomplish what you want (and a lot more -- extra
info in the titlebar) as:
######## ~/.zsh_titlebar ########
TITLE=$(print -n "\e]0;")
ENDTITLE=$(print -n "\007")
setopt promptsubst
TITLESEP='│' # ceçi n'est pas un pipe
[ -n "$NO_UTF8_TITLE" ] && TITLESEP='|'
function fancytitle () {
local c=$TITLESEP
printf "${TITLE}%s%s${ENDTITLE}" $1 "$(print -nP '%(!.ROOT $c .)%m $c %~ $c %n${STY+ $c $STY}')"
}
function preexec () {
if [ $SHLVL -eq 1 -a "$TERM" = "linux" ] ; then return ; fi
[[ -z "$NOTITLE" ]] || return
fancytitle "$1 $TITLESEP "
}
function precmd () {
if [ $SHLVL -eq 1 -a "$TERM" = "linux" ] ; then return ; fi
[[ -z "$NOTITLE" ]] || return
fancytitle ""
}
############## END ##############
The part you need is just:
function preexec () {
# this will change whenever you run a command
printf "\e]0;%s\007" $1
}
function precmd () {
printf "\e]0;%s\007" "whatever you want your shell title to be"
}
But, since you probably use Bash, check out:
http://www.macosxhints.com/dlfiles/preexec.bash.txt
where someone created scripts that emulate Zsh's precmd/preexec in Bash.
(And they include something similar to what my scripts above do.)
Best,
Ben |