Automatic meaningful titles for GNOME Terminal tabs

Viewed 144

(related: Identify gnome-terminal window by tab title)

When I open several tabs in GNOME Terminal (Version 3.44.0 for GNOME 42) on my Ubuntu 22.04, all tabs have the uninformative title "Terminal". I want them to be automatically titled:

  1. the current working directory if it's in shell prompt (e.g. /home/user123/Downloads)
  2. the name of the open process (e.g. vim ~/.bashrc) in case there is an open process.

How can I achieve that? I use bash, but I can switch to zsh if necessary.

bunch of tabs, all titled "Terminal"

1 Answers

From another thread somewhere on the internet:

function termtitle()
{
    printf "\033]0;$*\007";
}

I put this in my .bashrc right above my PS1Update(). Then I call it in PS1Update, right after I update the prompt. I call it with a string constructed from some env flags, username, hostname, pwd. You can put almost anything. I throw in $0. I did try using $PS1, but I use color and I think the printf in termtitle() trips over the escape sequences. So I build another string with the same info but w/o color.

Every time the prompt is updated, so it the terminal window/tab title, like if I change dir, ssh to another machine.

Hope this helps!

Related