Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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$CMD\e\\"
share|improve this question
up vote 7 down vote accepted

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.


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\\"

Then you can set a custom title by running:

TITLE="my title"

and unset the title by running


Don't forget to change precmd and $PS1 as well if necessary.


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:

    # determine the window title escape sequences
    case "$TERM" in
        # status line
        # window title
        if type tput >/dev/null 2>&1
            if tput longname >/dev/null 2>&1
                titlestart="$(tput tsl)"
                titlefinish="$(tput fsl)"

# or put it inside a case $- in *i* guard
if test -t 0; then

# set the xterm/screen/etc. title
    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}"
share|improve this answer
The command you give does exactly the same thing as Ctrl-A A. And about the prompt, you mean I have to modify $PS1 whenever I want the title not to change? – gregseth Feb 14 '11 at 11:45
I mean check it doesn't contain ESC k something ESC \. Ditto PROMPT_COMMAND. – Mikel Feb 14 '11 at 19:58
ESC could be written \e or \033. – Mikel Feb 14 '11 at 19:59
The screen title docs links should have the necessary information – Mikel Feb 14 '11 at 20:03

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 title\a, \e]2;new title\a or \eknew title\e\\ 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.

share|improve this answer
Thanks, you made it very clear. I have indeed found a preexec function defined in my .zshrc. The thing is, that's the behavior I want... most of the time. I'd like to be able to disable it on the fly on one specific screen. There's maybe a variable that I can get in the preexec function when I manually set the title, or something like that? – gregseth Feb 14 '11 at 21:50
@gregseth: It's a bit hard to explain in general, but if you post your preexec function, I or someone else can probably suggest a simple change. – Gilles Feb 14 '11 at 22:00
Thanks, I edited the post to add the function. – gregseth Feb 15 '11 at 10:33
I was going to mention preexec, but Greg didn't say he was using zsh. That'll teach me. ;-) – Mikel Feb 15 '11 at 22:33
@Mikel: My explanation was better than yours, but your new preexec design is better than mine. – Gilles Feb 15 '11 at 22:51

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.