116

Used to be able to right click on the tab and change the title. Not sure how to do this anymore. Just upgraded to Fedora 21.

EDIT: I have switched from gnome-terminal to ROXterm

0

12 Answers 12

159

Create a function in ~/.bashrc:

function set-title() {
  if [[ -z "$ORIG" ]]; then
    ORIG=$PS1
  fi
  TITLE="\[\e]2;$*\a\]"
  PS1=${ORIG}${TITLE}
}

Then use your new command to set the terminal title. It works with spaces in the name too

set-title my new tab title

It is possible to subsequently use set-title again (original PS1 is preserved as ORIG).

5
  • 6
    Please explain the details of how this works and why. Feb 7, 2020 at 6:37
  • 1
    Also, per this answer (stackoverflow.com/a/4824603/4561887) and many comments all around this answer, you should be using $@ instead of $* here to capture all input parameters to the call to set-title. Feb 7, 2020 at 6:48
  • 2
    I figured it out. See here for a detailed description, as well as cases where I demonstrate how to get dynamically-changing titles that change with the time or present working directory, for example: unix.stackexchange.com/questions/177572/…. Feb 7, 2020 at 22:50
  • 3
    @GabrielStaples No, "a$@b" expands to "aarg1" "arg2" "arg3" "arg4b" (several words), whereas "a$*b" expands to "aarg1 arg2 arg3 arg4b" (one word). In this case, $* is what we want (actually, the user should supply a single quoted argument, but to accept multiple arguments as a convenience $* is the appropriate solution in this specific case (even though in most cases $@ is the right option). Mar 22, 2021 at 15:45
  • Can you add an explanation how to include this in your usr/local/bin? I tried defining the function and then calling set-title "$@" in a shell script stored there, but that didn't work.
    – Natan
    Dec 20, 2022 at 9:01
52

The user title code was removed1 from gnome-terminal 3.14. To set the title, you could use an escape sequence:

printf "\e]2;YOUR TITLE GOES HERE\a"

or e.g. with bash:

PROMPT_COMMAND='echo -ne "\033]0;YOUR TITLE GOES HERE\007"'

1: see gnome bug 724110 and gnome bug 740188.

7
  • 17
    Thanks for a link to the discussions. I guess the answer to this was NO. A funny quote from one of the links: "modern users do not use terminals". LOL.
    – penner
    Jan 5, 2015 at 19:12
  • This doesn't work for me: Fedora 21, Bash 3.14.3.
    – kdbanman
    May 8, 2015 at 15:31
  • It works on fc22 also.. Jan 8, 2016 at 5:47
  • 6
    Apparently, it's coming back... "Yeah sorry, the --title option is restored in GNOME 3.20 (March 2016 release), which we released half a year ago, but Ubuntu 16.04 shipped with GNOME 3.18 (September 2015 release), which had removed this option." Michael Catanzaro's 2016-09-01 18:41:45 UTC comment #31 (from second bug, 740188)
    – sage
    Jan 12, 2017 at 22:10
  • 2
    @sage - there is no feature, Michael's comment there is about the --title option which I can confirm that was indeed restored. However, the Q here is about the right-click option to rename the currently open tab - a different issue... Jan 13, 2017 at 16:54
27

New versions of gnome-terminal just thrown away most helpful professional features. :-(

I have tried to setup and get an older version of gnome-terminal running and also compared alternatives.

If terminator is too exotic for you, the mate-terminal is a great option! It is a fork of gnome-terminal and just keeps all the good features:

  • you can open multiple tabs from the command line giving them different titles

    mate-terminal --tab -t "aaa" --tab -t "bbb" --tab -t "ccc"
    
  • you can set up a keyboard shortcut (I use Ctrl+Shift-i) to set a title

1
  • 1
    Guake is also not a bad variant
    – Bunyk
    Jan 12, 2018 at 10:00
21

My new script as of March 2021:

I have a new version of the "set title" function now. For the latest version of it, search my ~/.bash_aliases file somewhere around here. Here's what it might look like. It has a -h help menu now, and instead of relying on a backup of the PS1 variable the first time you run it, which is finicky, it simply uses the sed 's'tream 'ed'itor command and a regular expression to strip the existing title string from the PS1 variable instead.

gs_set_title() {
    CMD="gs_set_title"
    # Help menu
    if [ "$1" == "-h" ] || [ "$1" == "-?" ]; then
        echo "Set the title of your currently-opened terminal tab."
        echo "Usage:   $CMD any title you want"
        echo "   OR:   $CMD \"any title you want\""
        echo "   OR (to make a dynamic title which relies on variables or functions):"
        echo "         $CMD '\$(some_cmd)'"
        echo "     OR  $CMD '\${SOME_VARIABLE}'"
        echo "Examples:"
        echo "         1. static title"
        echo "           $CMD my new title"
        echo "         2. dynamic title"
        echo "           $CMD 'Current Directory is \"\$PWD\"'"
        echo "       OR  $CMD 'Date and time of last cmd is \"\$(date)\"'"
        return $EXIT_SUCCESS
    fi

    TITLE="$@"
    # Set the PS1 title escape sequence; see "Customizing the terminal window title" here:
    # https://wiki.archlinux.org/index.php/Bash/Prompt_customization#Customizing_the_terminal_window_title
    ESCAPED_TITLE="\[\e]2;${TITLE}\a\]"

    # Delete any existing title strings, if any, in the current PS1 variable. See my Q here:
    # https://askubuntu.com/questions/1310665/how-to-replace-terminal-title-using-sed-in-ps1-prompt-string
    PS1_NO_TITLE="$(echo "$PS1" | sed 's|\\\[\\e\]2;.*\\a\\\]||g')"
    PS1="${PS1_NO_TITLE}${ESCAPED_TITLE}"
}

Original answer:

@Weston Ganger wrote this function (and posted it here) to put into ~/.bashrc:

function set-title() {
  if [[ -z "$ORIG" ]]; then
    ORIG=$PS1
  fi
  TITLE="\[\e]2;$*\a\]"
  PS1=${ORIG}${TITLE}
}

You can then set a terminal title by calling set-title TERMINAL NAME (quotes around the name are optional).

It looked pretty mysterious to me (see my comments under his answer), so I spent a few hours last night reading and studying to figure out what the heck he had done and why it worked. Here's what I found:

  1. As of gnome-terminal 3.16.2 or so (see comments under this answer), "the option --title is no longer supported." Otherwise, you'd just do gnome-terminal --title="my title", like I used to do here.
  2. Per this answer here, and many comments all around this answer, you should be using $@ instead of $* to represent all input arguments in the script above. Apparently $@ is less bug-prone and more compatible, as it's the POSIX way to represent "all input arguments". Therefore, in my version below I use $@ instead of $*.
  3. It turns out that in nearly any terminal (so long as the given terminal supports it), there are ANSI escape codes, which are a form of "in-band signaling", which can be used to set a terminal title. See the section titled "Customizing the terminal window title" in this most-excellent archlinux wiki here. The Bash escape sequence to set the terminal title looks like this: \[\e]2;new title\a\], and to apply this title to your terminal window, all you have to do is modify its "Prompt String 1", or PS1 variable, by adding this "set title" escape sequence after your current Prompt String 1, like this: PS1="${PS1}\[\e]2;new title\a\]". Since gnome-terminal no longer supports the --title argument, this appears to be the only way to set the title anymore.

Now, here is my version of Weston Ganger's function, with extensive explanatory comments. This will be going into my dotfiles so I never lose it:

# Set the title string at the top of your current terminal window or terminal window tab
set-title() {
    # If the length of string stored in variable `PS1_BAK` is zero...
    # - See `man test` to know that `-z` means "the length of STRING is zero"
    if [[ -z "$PS1_BAK" ]]; then
        # Back up your current Bash Prompt String 1 (`PS1`) into a global backup variable `PS1_BAK`
        PS1_BAK=$PS1 
    fi

    # Set the title escape sequence string with this format: `\[\e]2;new title\a\]`
    # - See: https://wiki.archlinux.org/index.php/Bash/Prompt_customization#Customizing_the_terminal_window_title
    TITLE="\[\e]2;$@\a\]"
    # Now append the escaped title string to the end of your original `PS1` string (`PS1_BAK`), and set your
    # new `PS1` string to this new value
    PS1=${PS1_BAK}${TITLE}
}

Usage examples:

  • Static title strings (title remains fixed):
    • set-title my tab 1 OR set-title "my tab 1"
    • set-title $PWD OR set-title "$PWD"
  • Dynamic title strings (title updates each time you enter any terminal command): you may use function calls or variables within your title string and have them dynamically updated each time you enter a new command. Simply enter a command or access a global variable inside your title string. Be sure to use single quotes around the title string for this to work!:
    • set-title '$PWD' - this updates the title to the Present Working Directory every time you cd to a new directory!
    • set-title '$(date "+%m/%d/%Y - %k:%M:%S")' - this updates the title to the new date and time every time it changes and you enter a new terminal command! The format looks like this: 02/06/2020 - 23:32:58

Related:

  1. https://askubuntu.com/questions/315408/open-terminal-with-multiple-tabs-and-execute-application/1026563#1026563
  2. My dotfiles (config files, scripts, & generic user settings): https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles

Main References:

  1. [my question] https://askubuntu.com/questions/1310665/how-to-replace-terminal-title-using-sed-in-ps1-prompt-string
  2. How to rename terminal tab title in gnome-terminal?
  3. https://wiki.archlinux.org/index.php/Bash/Prompt_customization#Customizing_the_terminal_window_title
  4. https://www.thegeekstuff.com/2008/09/bash-shell-take-control-of-ps1-ps2-ps3-ps4-and-prompt_command/
  5. Why is bash's prompt variable called PS1?
  6. Bash Reference Manual: https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html
7
  • 1
    @AmanGupta, \[\e]2; is literally just a magic string delimiter sequence that means "this is the start of the terminal title in the terminal PS1 variable", and \a\] is literally just the string delimiter sequence that the makers of the Linux shell chose to mean "this is the end of the terminal title when setting the title string by writing it to the terminal PS1 variable". That's all there is to it. It's comparable to packetizing or serializing binary data packets over serial, for instance. You just choose some arbitrary 4-byte number to denote the start of a packet, and another... May 7, 2020 at 3:53
  • 1
    ...arbitrary 4-byte number to denote the end of the binary packet. Then, when searching through binary data you look for those markers to find a whole packet of data. This is the same with this textual string. They just chose some sequences to mark the beginning and end of the title string which is stored in the shell's PS1 (Prompt String 1) variable. May 7, 2020 at 3:53
  • 1
    Your effort is much appreciated, thanks! Aug 13, 2020 at 14:22
  • 1
    working in gnome-terminal 3.40.2
    – Spacemoose
    Jul 7, 2021 at 7:27
  • 1
    Thank you for your research. I learned a lot and improved on this answer into mine which is a one-liner. Dec 12, 2022 at 6:28
5

If you're using Ubuntu 16.04 you may need to:

PS1=$
PROMPT_COMMAND=
echo -en "\033]0;New title\a"

I list this an more info about it at link.

1
  • This destroys the old prompt text. Just use one line PS1="$PS1\[\e]0;New title\a\]" instead. Or better yet, setup as in my answer. Dec 12, 2022 at 6:22
4

I had the same problem and found that no easy way to set tab title from right click of mouse (the way I and most of my colleagues are used to). It was so irritiating at the sametime :(. So in our case, the solution was to switch the terminal. So search for alternatives like sakura, etc and finally settled on xfce-terminal, use the below command to install it

sudo apt-get install xfce4-terminal

It provides profile and the command to right click and change tab name. All other features are similar to Gnome

1

put this in .bashrc:

function title() { 
  p1='echo -ne "\033]0;' 
  p2='\007"'
  PROMPT_COMMAND=$p1$@$p2 
  p1= 
  p2=
}
1
  • And run this with 'title your new title'
    – B T
    Nov 13, 2019 at 20:02
1

When you run a resident program like top or ssh, the tab is properly labeled.

gnome-terminal --tab -e top -t "aaa" --tab -e top -t "bbb" 
1
1

For me -t parameter still works (gnome-terminal v3.36.1.1), but only while a command is executing:

gnome-terminal --tab -t browser-sync  -- npm run sync

so in the example above while browser-sync started by npm script is running - the title is there, and when it stopped.. then the tab is closed lol. Well if you do just

gnome-terminal --tab -t my-title

without command - you will still see 'my-title' in tab header but for fraction of second only.

1

Customize .bashrc so TERM_TITLE variable sets your terminal title

How to setup

Common case: .bashrc already sets terminal title

For the most cases (such as on Ubuntu), the default .bashrc has this line:

PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"

which sets terminal title everytime prompt text PS1 is displayed. The part between \[\e]0; and \a\] determines what the title is going to be. We are going to replace that part so the line becomes:

PS1="\[\e]0;\${TERM_TITLE:-${debian_chroot:+($debian_chroot)}\u@\h \w}\a\]$PS1"

This basically says:

  1. If variable TERM_TITLE is set and not null, then use its value as the title
  2. Otherwise, use the default title

Other cases

If your .bashrc doesn't already have such a line that contains PS1="\[\e]0;...\a\]$PS1", just add it.

Or, even better, switch the ordering around into PS1="$PS1\[\e]0;...\a\]" instead. This way, even if PS1 somehow already have a sequence that sets terminal title, we will overwrite it.

How to use

  • Set TERM_TITLE='new title' whenever you want terminal title to change.
  • Use unset TERM_TITLE or TERM_TITLE= to restore terminal title back to default.

Pitfalls

Note the escaped $ sign in \${TERM_TITLE:-...}. This means TERM_TITLE is evaluated when PS1 is processed for prompting.

If instead you write ${TERM_TITLE:-...} without the preceeding \, then TERM_TITLE will be evaluated when PS1 is set and the prompt will not update according to current value of TERM_TITLE.

0

As an expansion onto @Weston Ganger's answer, if you want to automatically set a title upon opening a new Gnome terminal, then add this to the bottom of your ~/.bashrc:

if [ ! -z "$SET_TITLE" ]; then
    set-title $SET_TITLE;
    export SET_TITLE=;
fi

Then launch a terminal like:

gnome-terminal --tab -e 'bash -c "export SET_TITLE=\"my title\"; bash -i"'

and it will automatically run set-title to apply the title.

0

one line solution, add following line in your .bashrc

alias tab_name='read -p "Name to Assign:" tabname ;  printf "\e]2;$tabname\a" '

now type tab_name in terminal i.e.

user:~$ tab_name  
Name to Assign: ( type desired name here! )

Done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .