229

I have several instances of terminals running in my working environment, what I would like is to set a specific title for each one, in order to have a clear idea what purpose the specific terminal serves i.e. Apache, editing_ini, postgres etc...

Of course from the command line.

6
  • Further to Ward's comment: Don't forget to "unset PROMPT_COMMAND" before you send the title-change escape sequence, otherwise any change you make will be lost as soon as the next shell prompt.
    – Jon Green
    Sep 2, 2011 at 11:38
  • For a similar question for KDE, see stackoverflow.com/questions/8782159/…
    – naught101
    May 29, 2012 at 2:22
  • I had this issue when switching from OS X to Ubuntu. To obtain custom titles within a multi-tab set-up, I used gnome-terminal --load-config together with this script I've written.
    – halfer
    Apr 13, 2014 at 1:29
  • 4
    add following to your .bashrc file in your home dir # set title of current terminal setTerminalTitle(){ echo -ne "\033]0;${1}\007" } alias termttl=setTerminalTitle now you can use termttl alias for setting title e.g. termttl askubuntu
    – urmalp
    Oct 11, 2015 at 5:33
  • 1
    "Protected" so I can't supply an answer. Gnome3 gnome-terminal default is PROMPT_COMMAND=__vte_prompt_command. This uses values from Profiles in prefs. When multiple profiles exist, New Tab and New Window menu items have a submenu item for each Profile. The manual way is to open a new terminal tab, right click on the tab title, and select Set Title.... (This would read so much easier in a separate answer, but ...)
    – Rich
    Aug 23, 2019 at 15:45

17 Answers 17

200

Alternatives:

  • There are other ways however, you can also issue

    gnome-terminal --title="SOME TITLE HERE"
    

    This might not give the desired effect since there is a big chance that your .bashrc overwrites that behaviour.

  • Bringing us to the last method, which I shamelessly ripped out of my .bashrc.

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

As an extra reference, this is the particular line in my .bashrc

PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD/$HOME/~}\007"'

You may also need to comment this code out in your ~/.bashrc

case "$TERM" in
xterm*|rxvt*)
    # JEFFYEE REMOVED because it makes commands to title() not work
    #PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac
16
  • 2
    Need to comment out or remove as you mentioned in order to take effect!
    – topless
    Jan 20, 2011 at 15:49
  • 38
    GNOME Terminal 3.16.2 says Option "--title" is no longer supported in this version of gnome-terminal.
    – Ivan Kozik
    Nov 18, 2015 at 21:23
  • 12
    On Gnome 3.18.3, there is no "Set title" entry in the menus :/
    – Shadi
    Sep 26, 2016 at 4:46
  • 5
    For anyone else confused by the gobbledegook in the strings above, they are ANSI escape characters - see e.g. lihaoyi.com/post/…
    – WillC
    Feb 24, 2017 at 0:21
  • 4
    GNOME terminal 3.20 does not have "set title..." in the menu Sep 13, 2017 at 6:11
98

Ward's answer is great if you want to set your title based on what host you're on etc every time you open a terminal. If you just want to quickly set a title though, you can just run echo by itself:

echo -ne "\033]0;SOME TITLE HERE\007"

or make a simple function (inside your ~/.bashrc), say termtitle

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

which you can run with termtitle some title here.

7
  • 4
    or similarly we can add as bash alias by adding below lines in ~/.bash_aliases function set_title() { echo -ne "\033]0;${1}\007" } alias title=set_title and then use: $ title term_title Sep 22, 2016 at 6:10
  • 18
    Sadly doesn't seem to work for me in GNOME terminal 3.6.2. Jan 26, 2017 at 23:36
  • 2
    Also doesn't seem to work in xterm 322 or konsole 16.12... I don't know if there's a new method, unfortunately :(
    – naught101
    Jan 27, 2017 at 0:49
  • 3
    @3ocene it doesn't actually matter in this case, they are the same thing, but it's a good habit to get into, because e.g. $var_log and ${var}_log are very different things, and the difference can cause bugs in scripts.
    – naught101
    Sep 10, 2017 at 10:50
  • 3
    if it doesn't work for you it is probably because PROMPT_COMMAND var or PS1 var is set and is over-riding it. unset PROMPT_COMMAND or set it so that it also changes the title e.g. export PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "$(basename $PWD)"'
    – gaoithe
    Aug 21, 2018 at 10:49
39

Argh, so many answers...

I tried wmctrl, which almost worked, except I couldn't get it to change the icon title, at least not permanently.

The problem is that the PS1 in Bash in Ubuntu sets the title.

The default PS1 is

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

... which sets the title in the first escape sequence: \e]0;\u@\h: \w\a

Thus, there are two solutions:

Solution 1: simplify PS1, then use PROMPT_COMMAND

Change PS1 to something simpler:

PS1="\u@\h:\w\$ "

Then use the PROMPT_COMMAND:

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

Solution 2: directly modify PS1

Simply modify PS1 with new title:

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

Notes on escape codes

Note that (borrowing from wjandrea's comment below this answer):

  • \e or \033 is the escape (ESC) character, which starts an escape sequence.
  • ] starts an operating system command (OSC).
  • For an xterm, 0; means "set the title", and
  • \a or \007 is the bell (BEL) character that terminates the OSC.

More info: https://en.wikipedia.org/wiki/ANSI_escape_code#Escape_sequences

7
  • hi, I like your answer quite a lot. Would you mind explaining the role of ]0; in your code? I think I found the meaning of echo -e '\007 \033' with the ascii -o command…although I’m truthfully not sure what that’s doing there either … thanks … Mar 10, 2017 at 11:28
  • 2
    @iso \e or \033 is the escape (ESC) character, which starts an escape sequence. ] starts an operating system command (OSC). For an xterm, 0; means "set the title", and \a or \007 is the bell (BEL) character that terminates the OSC. More info: en.wikipedia.org/wiki/ANSI_escape_code#Escape_sequences
    – wjandrea
    Feb 1, 2019 at 15:51
  • @wjandrea I was looking for the equivalent escape letters (\e and \a) for \033 and \007 but I couldn't find them certainly because I didn't know the right keywords to type in google. The link you gave is also useful. Can you please convert your comment to an answer ?
    – SebMa
    Apr 26, 2019 at 12:55
  • 1
    This should be the Accepted answer! It took me an hour of research to figure it out, why the accepted and the second answer did not work for me. (And it's my bad, because I red only the first two answers).
    – Aba
    Feb 7, 2021 at 18:39
  • A great, if not the best answer. Small correction: 0; means "set title and icon name". For the title only use 2;
    – MestreLion
    Nov 26, 2021 at 15:52
37

If you use the Vim editor, you can also enable this option in your vimrc:

:set title

which is disabled by default. It will set cool terminal titles showing the filename which you are editing at the moment and some other things.

1
  • this give name that will change every time not usefull :set notitle
    – ashish
    Sep 21, 2021 at 6:40
30

For the sake of completeness, I would add that you can also set the gnome-terminal title using this command:

wmctrl -r :ACTIVE: -N "MyWindowTitle"

You'll need to install the package wmctrl Install wmctrl first.

3
  • 2
    MyWindowTitle does not show up in title of terminal. It shows up when I do wmctrl -m.
    – sureshvv
    Aug 4, 2015 at 7:24
  • 5
    Note that wmctrl changes window title and not the tab title, whereas the escaped characters solution changes the tab title. Moreover, window title change back to original tab title as soon as you switch between tabs.
    – redochka
    Nov 13, 2015 at 19:32
  • This didn't work for me with Ubuntu 16.04 LTS. Mar 27, 2021 at 3:23
29

Works on Ubuntu 16.04 to 22.10

This answer is simpler than most others. To use it, you would just type:

title "My new title"

  • Easier to remember than most other answers.
  • The accepted answer and others do not work on modern versions of Ubuntu (from 16.04 on up).
  • This answer doesn't require 3rd party packages like wmctrl and xdotool.
  • Version 2 allows running multiple times in the same session.

One-time function creation

Create the title function in your ~/.bashrc file:

function title() {
    # Set terminal tab title. Usage: title "new tab name"
    prefix=${PS1%%\\a*}                  # Everything before: \a
    search=${prefix##*;}                 # Eeverything after: ;
    esearch="${search//\\/\\\\}"         # Change \ to \\ in old title
    PS1="${PS1/$esearch/$@}"             # Search and replace old with new
}

Save the ~/.bashrc file. After opening a new terminal tab use:

title "Special Projects"

or:

title Special\ Projects

A phrase with spaces must be wrapped in double quotes (") or each space must be escaped with \.

10
  • 3
    Thanks! This is the solution that worked for me with GNOME terminal 3.36.2.
    – mr.adam
    Aug 13, 2020 at 1:53
  • 1
    Thank you, it works with Gnome Terminal 3.36.2 but only once. I mean after you set the title, you can't change it anymore. Mar 25, 2021 at 6:18
  • 2
    @MinhNguyen I've upgraded function to allow renaming multiple times per session. Thank you for your suggestion. Mar 27, 2021 at 15:17
  • 1
    @WinEunuuchs2Unix. It works like a charm. Thank you very much. Mar 29, 2021 at 5:11
  • 2
    Thanks! This worked on GNOME terminal 3.38.1. Renaming worked also! (Upvoted.) Aug 20, 2021 at 19:02
9

If you are a Ubuntu user, you can change the title of a gnome-terminal tab using the HUD.

While in the gnome-terminal, hit Alt to bring up the HUD, type the first few letters, e.g. "tit", hit enter and type in your new title.

This is a very quick method and avoids using the mouse.

(UPDATE: In newer versions of gnome-terminal, this was removed. Try other answers.)

5
  • 1
    Indeed. Keeps the hands on the keyboard. The existing title is displayed, so it's fast and easy to also just modify the existing one using this method. Dec 19, 2013 at 0:39
  • tit is only for English Ubuntu. Feb 22, 2017 at 13:02
  • 2
    and the whole option has been removed :( Feb 22, 2017 at 13:02
  • the F2 key also avoids the mouse. Mar 10, 2017 at 11:31
  • @törzsmókus And French - titre ;)
    – wjandrea
    Feb 1, 2019 at 15:56
3

Another way of changing the title of gnome-terminal is by using gconftool-2; this changes the initial terminal title for the profile selected, so you could have different profiles associated with titles such as 'Apache', 'Editing', etc. You would then launch gnome-terminal with the appropriate profile to get the terminal title you had specified. This is in contrast to gnome-terminal --title "name" which changes the title per terminal, but doesn't affect the initial title specified in the profile.

You could use the following command in a script to set the name of the terminal for a profile, and you could have the name of the terminal change at certain times in the day to remind you of things:

gconftool-2 --set /apps/gnome-terminal/profiles/Default/title --type=string "Apache"

This is for the default profile, but you could set the title for other profiles as well by changing, for example, Default to another profile like Profile0:

gconftool-2 --set /apps/gnome-terminal/profiles/Profile0/title --type=string "Editing"

I thought this way of changing the title is of use because of the way it could be used in scripting, or just as a quick command-line way to set the title for the profile. Note that sometimes you have to relaunch the terminal with the specified profile for the gconftool-2 setting to take affect. The complete settings available for gnome-terminal can be listed with gconftool-2 -R /apps/gnome-terminal.

3

To display only the current working directory in the title, try this in your '.bashrc' :

PROMPT_COMMAND='echo -ne "\033]0; ${PWD##*/}\007"'

or

PROMPT_COMMAND='echo -ne "\033]0;$(basename ${PWD})\007"' 
3

Another solution is to use xdotool to simulate keystrokes, maybe useful in scripts:

  1. Set a keyboard shortcut in gnome-terminal:

    Edit > Keyboard Shortcuts... > Terminal > Set Title
    

    For example assing the Shift+Ctrl+Y.

  2. Install xdotool if you don't have it already:

    sudo apt-get install xdotool
    
  3. The following sequence of commands (that you can use also in a bash script) will set the terminal/tab title (escape the spaces with \):

    xdotool key ctrl+shift+y 
    xdotool type My\ new\ fabulous\ title
    xdotool key Return
    
  4. [optional] You can also use xdotool to e.g. open a new tab and set the title with the above commands, using:

    xdotool key ctrl+shift+t
    

    Consider adding a sleep time before and after opening a new tab, e.g. sleep 1 (to wait for 1 second).

This is not the most elegant solution, but it worked for me! The previous answers did not work in my case. I use gnome-terminal in Ubuntu 14.04 and I wanted to make a bash script.

2

This worked in my Gnome Terminal 3.18.3.

Edit your .bashrc file and add this function

# Update gnome terminal title
function termtitle() {
    # take argument
    TITLE=$1
    shift
    # update title
    PROMPT_COMMAND='echo -ne "\033]0; $TITLE \007"'
}

Don't forget to source your .bashrc file

$ source ~/.bashrc

And then you can simply update you'll be able to change terminal title like this:

$ termtitle "MariaDB CLI"
2
  • 1
    On stock Ubuntu 20.04 with preinstalled gnome-terminal 3.36.2 using VTE 0.60.3 this does not do anything. This other similar answer however works.
    – Levente
    Apr 7, 2021 at 2:19
  • worked on my Ubuntu 20.04
    – tgkprog
    Sep 6, 2021 at 18:48
2

easy Ubuntu 20.04 way (*from https://askubuntu.com/a/1366329/149527)

PS1=$PS1"[\e]0;New_Terminal_Name\a]"

2
  • This did the trick for me: PS1=$PS1"\e]0;New_Terminal_Name\a"
    – sanmai
    Aug 5, 2022 at 8:53
  • This worked for me on Ubuntu 22.04.3 LTS, however it causes problems with my prompt as mentioned here. Overall a good quick and dirty solution
    – kyrlon
    Jan 22 at 18:05
1

In case any poor souls like myself are forced to use tcsh, try something like this in your .cshrc:

set prompt='%{\033]0;%n@%m\007%}%~%# '

Source and more details: http://tldp.org/HOWTO/Xterm-Title-4.html

1
  • The question was asking how to set the title to a custom value, not have it automatically generated from the prompt. Aug 11, 2023 at 17:47
1

If you are using gnome 3, the convenient Terminal > Set Title menu option has been removed. You can install a gnome 2 forked terminal which still has that "Set Title" menu option, and run that instead:

sudo apt-get install mate-terminal

Instead of running "Terminal", run "MATE Terminal". You should probably also select mate-terminal in sudo update-alternatives --config x-terminal-emulator.

1

As an addition to other valid answers, xdotool has the functionality that allows you to change the title of any application and window.

xdotool getactivewindow set_window --name "WINDOW NAME HERE"

Note that the change won't be permanent, it'll change when you change directory and probably be set to your PWD.

To permanently change it, you can modify PS1 variable within ~/.bashrc as mentioned.

PS1="${PS1}\[\e]0;New Title\a\]"

This modification is permanent. In fact after this, the changes you make with xdotool will also be permanent and won't change with cd.

1

for a wsl (windows subsystem for linux) terminal tab i'll do this:

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

In your ~/.vimrc file along with the

set title

command

add the line

set titleold=

The old window title will reappear when you exit vim. It was already thought of!

Also, people who have been using vi since the 80's may find that using an arrow key does not bump you into command mode. Add this to ~/.vimrc if you want 80's terminal behavior

" vi users may expect that if you are 
" in INSERT mode if an arrow key is pressed it will hop back 
" into command mode. 
"
inoremap <left> <esc><left>
inoremap <right> <esc><right>
inoremap <up> <esc><up>
inoremap <down> <esc><down>
1
  • 1
    The question is not about vim, it's about terminals in general. For example, one might want to set the title without launching vim (or any other application) at all. Oct 25, 2022 at 0:00

You must log in to answer this question.

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