By default, the title of a screen session is the name of the last command entered, which is fine for me, but in some cases I’d like to change it. I know the command Ctrl–A A, but it only changes the title until the next command, and I’d like it to stay there until I decide otherwise.
EDIT:
Here’s the preexec function I found in my .zshrc
if [[ "$TERM" == "screen" ]]; then
local CMD=${1[(wr)^(*=*|sudo|-*)]}
echo -n "ek$CMDe\"
fi
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Depends how things are set up, but by default, something like this should work.
settitle() {
printf "33k$133\"
}
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 33 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='33]0;'
titlefinish='07'
;;
cygwin)
titlestart='33];'
titlefinish='07'
;;
konsole)
titlestart='33]30;'
titlefinish='07'
;;
screen*)
# status line
#titlestart='33_'
#titlefinish='33'
# window title
titlestart='33k'
titlefinish='33'
;;
*)
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
Method 2
If your window title reflects the application currently running in the window, it’s because your shell is doing that. If it’s doing that, it’s because your distribution, your system administrator or you configured it to.
If the command appears as the window title while the command is running, this is done by the preexec function (in zsh), or an emulation thereof under bash.
If the title changes when each command completes, this is done by the precmd function in zsh, the PROMPT_COMMAND parameter in bash, or as part of the prompt (PS1 parameter) in any shell. (The command to change the title is an escape sequence emitted by any application as part of the terminal’s output stream, so it can be embedded in a prompt.)
Look for an escape sequence like e]0;new titlea, e]2;new titlea or eknew titlee\ in your shell initialization file (~/.bashrc, ~/.zshrc, …). If there’s nothing relevant, the setting may be in a system file (e.g. /etc/bash.bashrc, /etc/zsh/zshrc); then overwrite that parameter or function in your own initialization file.
See Mikel’s answer for an overridable title setting in preexec.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0