GNU screen hacking question
Ben Scott
dragonhawk at gmail.com
Sun Jun 10 00:15:30 EDT 2007
On 6/8/07, Paul Lussier <p.lussier at comcast.net> wrote:
>> Hmmm. To turn the question on it's head: Does anyone know if it's
>> possible to set the title of a screen from the encapsulated program?
>
> Are you trying to have the command run within screen do the equivalent of
> setting 'C-a A:foo' (or screen -t foo) ?
Yes, exactly. Michael O'Donnell's clue worked (thanks!), once I
figured out that the trailing backslash was actually part of the
escape sequence. :)
Now my xrename_once shell function can also set the screen window
title, as well as the xterm window title. This is especially cool,
because my PROMPT_COMMAND uses xrename_once to reflect the current
directory. I also have some commands set up with aliases which rename
the window while the command is running (for example, invoking "less"
will title the window with the files I'm viewing).
The practical upshot is that my screen sessions will now
automatically re-title themselves to reflect what I'm currently doing.
The following are extracts from my newly-updated shell startup
files. (Note that I use multiple files, source'd, and make use of the
return built-in, so you won't be able to cut-and-paste this into a
single .bashrc without a little modification. But it's a start. I
can anything explain further if anyone is interested.)
# ----------------------------------------
# window title control
function xrename_once () {
if [ $TERM = xterm ]; then
echo -ne "\e]0;$1\a"
elif [ $TERM = screen ]; then
echo -ne "\ek$1\e\\"
fi
}
function xrename () {
unset PROMPT_COMMAND
xrename_once "$1"
}
# ----------------------------------------
# REMOTE_HOST
# sets REMOTE_HOST to name or address of system we logged in from, if any
# don't bother if already set
[ -n "$REMOTE_HOST" ] && return
# SSH (new form)
if [ -n "$SSH_CONNECTION" ]; then
export REMOTE_HOST="${SSH_CONNECTION%% *}"
return
fi
# SSH (old form)
if [ -n "$SSH_CLIENT" ]; then
export REMOTE_HOST="${SSH_CLIENT%% *}"
return
fi
# ----------------------------------------
# prompt magic
# set PS1 based on local vs remote login
# if this is a remote login...
if [ -n "$REMOTE_HOST" ]; then
# include hostname in prompt as a reminder
PS1='\h\$ '
else # login to local host...
# no host in prompt means local host
PS1='\$ '
fi
unset title_term
[ "$TERM" = xterm ] && title_term=1
[ "$TERM" = screen ] && title_term=1
# if we cannot title the terminal, skip the rest
if [ -z "$title_term" ]; then
unset PROMPT_COMMAND
export PROMPT_COMMAND
return
fi
# now set terminal title
# username is not always the same...
username="$USER"
# make username ALL CAPS for superuser
[ "$username" = root ] && username=ROOT
# is this me?
unset is_me
[ "$username" = bscott ] && is_me=1
unset prefix
# if it's not me...
if [ -z "$is_me" ]; then
# if not me, always include "user at host:"
prefix="${username}@"'${HOSTNAME%%.*}:'
elif [ -n "$REMOTE_HOST" ]; then
# if me but remote, include "host:"
prefix='${HOSTNAME%%.*}:'
fi
# else: me on local host, so blank prefix
# multiple levels of escapes make this messy
# want $prefix to expand when PROMPT_COMMAND is set
# when PROMPT_COMMAND runs:
# want $PWD to expand
# want $HOME's expansion to be pattern for substitution
# want literal ~ to be replacement for substitution
PROMPT_COMMAND="xrename_once \"${prefix}\${PWD/#\$HOME/~}\""
# ----------------------------------------
# titled commands
alias less='run_titled less'
alias jmacs='run_titled jmacs'
function run_titled () {
# sets xterm title to arguments of command, or command name if not args
local cmd title
cmd="$1"
shift
if [ $# = 0 ]; then
# bare command, just use command
title="$cmd"
elif [ ${1:0:1} = - ]; then
# first arg was a switch, include everything for clarity
title="$cmd $*"
else
# just arguments
title="$*"
fi
xrename_once "$title"
command "$cmd" "$@"
}
function man () {
# special case of run_titled for "man 1 foo" as "foo(1)", etc.
local title
if [ $# = 1 ]; then
title="$1()"
elif [ $# = 2 ]; then
if [ ${1:0:1} = - ]; then
title="man $*"
else
title="$2($1)"
fi
else
title="man $*"
fi
xrename_once "$title"
#LANG=en_US.utf8 command man "$@"
command man "$@"
}
More information about the gnhlug-discuss
mailing list