How to customize Ubuntu's terminal tab titles

Posted on: Sunday, Dec 09, 2018

Ubuntu's default terminal title display is a little verbose. It includes the current user and machine name and the current directory name (which is chopped off of the right end of the directory name). This default title text is never any help! When you have three or four terminal tabs running all four are titled exactly the same (as shown below in Figure 1a):

Ubuntu standard terminal tab title

Figure 1a. A three-tab terminal with standard tab titles

I wanted to eliminate the user and machine name and truncate the current directory name from the left (as shown below in Figure 1b):

Ubuntu custom terminal tab title

Figure 1b. A three-tab terminal with titles showing truncated (from the left) directory names

Using the zsh configuration below the terminal titles in Figure 1b show only the right-hand side of the current directory (showing the right-most 24 characters works best for me). Care must be taken when SSHing or using Docker to exec into into a server with the terminal because the title no long differentiates machine names--but those tabs can be easily changed at runtime on most terminals.

These instructions are for the zsh shell, but the process would be similar for bash. This process works for most terminal consoles. It works well for me with both Terminator and Guake.

No scripting needed

I thought this challenge was going to require some sophisticated zsh scripting. But a little reading proved otherwise. This page provided the clues I needed. It directly showed how to remove the user and machine name from the prompt and linked to this page which provided additional macro expansions for setting the terminal value title value. The obtuse macro expansion needed is:

%x<..<%/ 

where x is the number of characters to truncate (from the left side of the directory name string). This macro expansion takes care of all of the dirty work; if the string isn't x characters long it isn't truncated and if it is longer, it is truncated with two dots prepended to it.

Hooking up this all required adding the code below in Figure 2 to the .zshrc file (in my home directory). The code below looks like it's missing an opening paren, it isn't. zsh/Bash scripting can be downright weird.

Add this code to the ~/.zshrc file.
case $TERM in
    xterm*)
     precmd ()  {
        print -Pn "\e]0; %24<..<%/\a"
     }
     ;;
esac

Figure 2. Change needed to the .zshrc file. 

If you want more or less than 24 characters showing, change the 24 embedded in the code above to whatever value works for you.

It was also necessary to set the DISABLE_AUTO_TITLE value in .zshrc file to true. Don't forget this! The technique doesn't work without it.

DISABLE_AUTO_TITLE="true"



Add your comment
You email is never shared with anyone else.

© Copyright 2017 by Roger Pence. All rights reserved.