How to change Terminal Title in ubuntu 16.04

35,116

Solution 1

A lot of programs will overwrite the title so you can't rely on the title being fixed or not duplicated by other windows. This is especially so with remote ssh sessions in a gnome-terminal. It still helps a lot but its not reliable enough for window managers to do matching against (which is why I think they removed it. (addition by Amias Channer)) so this ability has been taken out with the newest gnome-terminal, however there is still a possibility to change the title, you can do it by command. To achieve this easily edit your ~/.bashrc file and add the following lines:

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

With this you then can set the title of your terminal window by simply using the command set-title <name you want to set it to>. This is possible due to ANSI escape codes so any program can output them regardless of where the code is run. That's what the \e and \a bits do. (addition by Amias Channer).

The solution I found here and using it myself since I run on 16.04 LTS.

Solution 2

Videonauth's solution is bash-specific, meaning that if you use some other shell (such as korn shell or c shell or mksh or tcsh), it won't work. It also sets title via editing the PS1 prompt ( which for some reason has effect on the title in bash). Here's for example gnome-terminal with mksh:

enter image description here

What I personally use is this:

setTitle() {
    echo -e "\033]0;$@\007" 
}

This command uses escape sequences and is shell-agnostic , meaning this works in shells other than bash. Small downside is that you will need to know ASCII escape sequences if you want to tweak this.

Solution 3

This is the function I use in my ~/.bashrc file:

function termtitle() { PS1="${PS1/\\u@\\h: \\w/$@}"; }

After adding it (or changing it) you must resource your file:

. ~/.bashrc

To use it type something like:

termtitle Special Projects

After changing the title once, you must resource to change it again:

. ~/.bashrc
termtitle My new title
Share:
35,116

Related videos on Youtube

Vikrant
Author by

Vikrant

Updated on September 18, 2022

Comments

  • Vikrant
    Vikrant over 1 year

    When I was using ubuntu 14.04, it's easy to set title. Just press F2 or move mouse to top menu. But there is no menu on ubuntu 16.04 and press F2 can't change the title. What should I do?

  • Amias
    Amias almost 8 years
    A lot of programs will overwrite the title so you can't rely on the title being fixed or not duplicated by other windows. This is especially so with remote ssh sessions in a gnome-terminal. It still helps a lot but its not reliable enough for window managers to do matching against which is why I think they removed it.
  • Videonauth
    Videonauth almost 8 years
    Interesting information, if you dont mind i add it into my answer as it is.
  • Amias
    Amias almost 8 years
    If you like, the title is set by ANSI escape codes so any program can output them regardless of where the code is run
  • Amias
    Amias almost 8 years
    That's what the \e and \a bits do
  • MikeW
    MikeW over 7 years
    Still, it's a bit arrogant to remove a useful feature - surely "we know what we're doing" !!
  • WillC
    WillC about 7 years
    Hmm; if programs renaming the window caused problems, and duplicate titles cause problems, then a possible solution would be -- 1) for the terminal to recognise duplicate titles and suffix with _1, _2, etc to make them unique, and return the title value set for use in scripting -- and 2) add per-tab and per-window flags which can be set to block subsequent renaming
  • amflare
    amflare about 7 years
    Thanks for this solution @Videonauth, one question for you. I'm curious what the regex-ish looking code is following TITLE=. Could you explain it, or at least point me in the right direction so I can find information online? Thanks.
  • Videonauth
    Videonauth about 7 years
    @amflare The so reg-ex looking code are ANSI escape sequences (I would start looking there for more insight), its just looking so weird because you cant type them in normally and you have to escape many of the characters for bash like the [ which will become escaped \[.
  • Pie
    Pie over 4 years
  • Noumenon
    Noumenon over 4 years
    I have a follow-up question about using this in a script to signal that the script is running or complete. I'm having trouble because this function takes effect only once all commands on the current command line are complete, not immediately. askubuntu.com/questions/1193033/…
  • MestreLion
    MestreLion over 2 years
    This is the best answer. I have no idea why others fiddle with PS1 when it's not needed at all.
  • MestreLion
    MestreLion over 2 years
    That said, I suggest changing the escape sequence to echo -ne "\e]2;$@\a" instead, so it sets just the title and not title and icon name