Change Unix terminal Title

Changing the Terminal Window Title Text

To change Linux (and perhaps other systems) Terminal windows' titles, you must use escape codes as follows:
printf "\e]2;YOUR TITLE GOES HERE\a"
If you execute this in a terminal session, it will change the title of the terminal to “YOUR TITLE GOES HERE”
If you omit a title string (i.e. use printf "\e]2;\a"), the title will default to “Terminal”.

Correcting PS1 From Resetting The Title

There is one situation in which the above may seem not to work: when the screen title is set within the Unix prompt construction.

How The Unix Terminal Prompt is Set

The in a Shell, the PS1 environment variable controls what texts is shown at the prompt. Every time a shell command completes and the prompt is shown, PS1 is evaluated. Since it is a shell variable, anything that can be done in the context of shell variables is possible with the PS1 prompt - in fact arbitrary shell code is possible to be run during the evaluation of a variable giving PS1 great flexibility. In particular, the window title can be set.
A typical (current as of 2020) PS1 variable is something like:
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$
It is the highlighted first section whose action is to set the title.

Correcting PS1

The section that changes the title (\[\e]0;\u@\h: \w\a\]) must be removed prior to the above technique being used. To have no other effect, the following should be run:
export PS1="${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ "
Then the window title change can be done:
printf "\e]2;YOUR TITLE GOES HERE\a"

Comments

Popular posts from this blog

TEX on Linux

Freeze Changing HTML Element in Chrome Developer Tools