Is there a way to present the GNU Screen session name and window title in the prompt of the shell (let us say, the Bash prompt defined by PS1
)?
Bash – Showing GNU screen session name and window title in shell prompt
bashgnu-screenprompt
Related Solutions
Depends how things are set up, but by default, something like this should work.
settitle() {
printf "\033k$1\033\\"
}
Then run:
settitle NEWTITLE
.
See screen title docs and GNU screen faq for more details.
Given Ctrl+A A is only changing it until the next command, it's probably being set by $PS1
(all shells), or $PROMPT_COMMAND
/DEBUG trap
(bash only) or precmd
/preexec
(zsh only).
You should look for any place that \e
or \033
appears with k
or \\
after it, basically like my settitle
example above.
UPDATE
You said you had a custom preexec
.
Why not change it to this:
if [[ "$TERM" == "screen" ]]; then
local CMD=${1[(wr)^(*=*|sudo|-*)]}
echo -n "\ek${TITLE:-$CMD}\e\\"
fi
Then you can set a custom title by running:
TITLE="my title"
and unset the title by running
TITLE=
Don't forget to change precmd
and $PS1
as well if necessary.
ASIDE
You could even extend this to all terminals (e.g. xterm
, gnome-terminal
, etc.) by not hard coding the \ek
and \e\\
.
This is how I do it:
terminit()
{
# determine the window title escape sequences
case "$TERM" in
aixterm|dtterm|putty|rxvt|xterm*)
titlestart='\033]0;'
titlefinish='\007'
;;
cygwin)
titlestart='\033];'
titlefinish='\007'
;;
konsole)
titlestart='\033]30;'
titlefinish='\007'
;;
screen*)
# status line
#titlestart='\033_'
#titlefinish='\033\'
# window title
titlestart='\033k'
titlefinish='\033\'
;;
*)
if type tput >/dev/null 2>&1
then
if tput longname >/dev/null 2>&1
then
titlestart="$(tput tsl)"
titlefinish="$(tput fsl)"
fi
else
titlestart=''
titlefinish=''
fi
;;
esac
}
# or put it inside a case $- in *i* guard
if test -t 0; then
terminit
fi
# set the xterm/screen/etc. title
settitle()
{
test -z "${titlestart}" && return 0
printf "${titlestart}$*${titlefinish}"
}
Then you can change your preexec
to:
if [[ "$TERM" == "screen" ]]; then
local CMD=${1[(wr)^(*=*|sudo|-*)]}
settitle "${TITLE:-$CMD}"
fi
An incomplete solution would be to modify your $PS1
prompt once inside a GNU screen. Start by modifying your shell's RC file (ie ~/.bashrc
). Look for a case statement that evaluates $TERM
:
case "$TERM" in
xterm*|rxvt*)
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
;;
*)
;;
esac
Add the following prior to the catch all (ie *)
):
screen)
PS1='\e]0;\u@\h:\w\a\ek\u@\h:\w\e\\\u@\h:\w$(__git_ps1 "(%s)")\$ '
;;
This will modify your hardstatus: \e]0;...\a
This will modify your window title: \ek...\e\\
The hardstatus will not show up unless you have configured it to do so. For example, you could add the following to your .screenrc
:
hardstatus alwayslastline
Complex hardstatus string
options will continue to work. You can substitute your now dynamic stored hardstatus for the current window using %h
:
hardstatus string '%{= kw}[ %h ] %=%{w}[ %{r}%l%{w} ]%{w}[%{y} %Y-%m-%d %C %A %{w}]%{w}'
This will set the hardstatus line to the stored hardstatus between two brackets on the left side of the screen and the CPU utilization to the right along with the date and time.
Example:
[ username@host:~ ] [ 0.00 0.01 0.00 ][ 2012-11-27 4:13 PM ]
CAVEATS
This will most likely break your cursor offset in your shell's history recall. (Hit your up arrow a few times, then try to edit that line. You'll see what I mean.) This is because most shells use the length for
$PS1
, and this will include the non-printable characters added to$PS1
.This will most likely break the xterm title bar, "freezing" it to the last status set prior to running GNU screen. What is interesting is that applications like vim that update the title bar will continue to work.
Manually setting the screen title (ie
C-A A
) will still work until the command prompt is updated.
Best Answer
Screen supplies some environment variables (from
screen
(1) manpage):The "at creation time" means that if you renumber a window (using screen's
number
command), the shell will not be told about the change and $WINDOW will still be the same as the first window number.You could use something like: