How to Change the Title of the Window of GNOME Terminal on Linux Mint

Share
In this tutorial, we'll learn how to change the title of the terminal window of GNOME Terminal and similar terminal emulators on Linux Mint, both temporarily and permanently by customizing our .bashrc file.

Command to Change the Title

Although there is no terminal command for changing the title, on a xterm and compatible terminals (including GNOME Terminal), when the terminal sees a specific text printed by a program, it changes the title of the window. This may sound weird at first glance, but that's because the "text" we're talking about is a sequence that starts with non-printable characters.

Specifically, when xterm sees the sequence of 4 character ESC, ], 0, ;, whatever comes after becomes the title, up until a BEL character1. These characters, ESC and BEL, are non-printable ASCII characters. For example, ESC represents the Escape key, but pressing the Escape key normally doesn't type anything, so normally it's impossible to type this character, and because of that they can be used in this special way.

We can't print these characters using echo, but we can do it using the terminal command printf. In this command, the ESC character can be represented as \e, while the BEL character can be represented as \a. This means if we type:

printf "\e]0;Blink\a"

We might see the title of the window change for an instant if we're lucky before it changes back.

The reason it changes back is that when printf terminates, the shell prompt will be printed. Normally we only see the visible characters of the shell prompt, like john@pc:~$ . However, the shell prompt also includes hidden characters just like the above that are printed every time the shell prompt appears. This means that when the shell prompt appears, the title is reverted to its default.

There is a trick we can use to make it work. The sleep command simply waits a number of seconds before terminating. While it hasn't terminated, the shell prompt won't be printed, which means we can do this:

printf "\e]0;Sleeping zzzzzz....\a" && sleep 3

The && operator means that after the first command the second command will be executed, and only after the second command the shell prompt will appear. Doing this, we should see the title change for 3 seconds before the shell prompt appears and reverts the title to the default.

Changing the Title Permanently Using .bashrc

To change the title permanently, the easiest method is to change the shell prompt to print the title we want. Then, every time the shell prompt appears, the title will be reset to what we defined.

GNOME Terminal uses the PS1 variable to tell what the shell prompt should be. This PS1 variable is defined on Linux Mint's default .bashrc like this:

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

This looks a bit complicated. Essentially, it checks if color is supported in the shell prompt, and in that case includes special code in the PS1 variable for color. Otherwise, it sets PS1 as simple as possible. Then, if the terminal is compatible with xterm, it includes the code to set the title before the existing value of PS1.

Let's add a customization for this.

If you haven't already, see [How to Add Customizations to .bashrc on Linux Mint] for best practices.

The code that we want to add is the following:

# If this is an xterm set the title to the current date
case "$TERM" in
xterm*|rxvt*)
    PS1=${PS1#*'\]'}
    PS1="\[\e]0;\$(date)\a\]$PS1"
    ;;
*)
    ;;
esac

This code does two things. First, sets PS1 to what Linux Mint defined for PS1, except all characters up to a \]. In other words, it removes the start of PS1 all the way to the \], this way we can replace it without having to recreate the color code for the shell prompt.

Then, it sets prefixes PS1 with a similar code to set the title, but our title this time is \$(date). The \$ is necessary to avoid Bash from replacing $(date) by the current date the instant .bashrc is loaded. We want date to be executed every time the shell prompt is shown instead, so it always shows the current second.

If we save this customization and then open the terminal, we should see that the title is always the current date now.

References

Written by Noel Santos.

About the Author

I'm a self-taught Brazilian programmer graduated in IT from a FATEC. In a world of increasingly complex and essential computers, I decided to use my technical expertise in hardware, desktop applications, and web technologies to create an informative resource to make PC's easier to understand.

View Comments