How to change Gnome-Terminal title?

  • 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.

    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.

    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.

    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`

    "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 ...)

  • 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)}\[email protected]\h: \w\a\]$PS1"
        ;;
    *)
        ;;
    esac
    

    Need to comment out or remove as you mentioned in order to take effect!

    @DoR the code PROMPT_COMMAND=... I commented it out. If not removed from the .bashrc then the title remains the same since its getting override by the .bashrc settings.

    @DoR That seems odd given `PS1` is the variable for the actual prompt. Hmm, that makes me wonder why the one for the title gets called `PROMPT_COMMAND`. Anyone know anything about that?

    @Ward: `PROMPT_COMMAND` is a *program* to run. `PS1` and `PS1` are *textual strings* that are displayed. In the example above the command that is run is `echo`, which then simply prints out a string. See `man bash` or http://manpages.ubuntu.com/bash for more details.

    I wonder why this `--title` option is not documented in `gnome-terminal --help`!

    @umpirsky it is in `--help-all`

    note, if you do not see the menu bar in the terminal, you may activate it by right-clicking in the terminal and checking `Show Menubar`

    GNOME Terminal 3.16.2 says `Option "--title" is no longer supported in this version of gnome-terminal.`

    I had to escape the `~` in `${PWD/$HOME/~}` to get the home directory replaced: `${PWD/$HOME/\~}`.

    On Gnome 3.18.3, there is no "Set title" entry in the menus :/

    For anyone else confused by the gobbledegook in the strings above, they are ANSI escape characters - see e.g. http://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html

    http://askubuntu.com/a/774543/455406 sets the title programmatically; it is bash-specific.

    GNOME terminal 3.20 does not have "set title..." in the menu

    @WardMuylaert Any way to have it open a new tab instead of anew window?

    @WillC Thanks for that link, it's the only programmatic answer on this page that worked!

  • 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.

    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`

    Sadly doesn't seem to work for me in GNOME terminal 3.6.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 :(

    Why do you have to use `${1}` instead of `$1`?

    @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.

    I guess this only works if your bash prompt does not overwrite it again. Other terminals (such as MATE terminal, or KDE konsole) support overwriting the term title on the GUI level even if the shell sets a title. I guess the real answer to this question should be GNOME terminal simply does not support it

    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;%[email protected]%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "$(basename $PWD)"'```

  • 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.

  • 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.

    MyWindowTitle does not show up in title of terminal. It shows up when I do wmctrl -m.

    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.

  • 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;\[email protected]\h: \w\a\]${debian_chroot:+($debian_chroot)}\[email protected]\h:\w\$ 
    

    ... which sets the title in the first escape sequence: \e]0;\[email protected]\h: \w\a

    Thus, there are two solutions:

    Solution 1: simplify PS1, then use PROMPT_COMMAND

    Change PS1 to something simpler:

    PS1="\[email protected]\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)}\[email protected]\h:\w\$ '
    

    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 …

    @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: https://en.wikipedia.org/wiki/ANSI_escape_code#Escape_sequences

    @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 ?

  • 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.

    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.

    _tit_ is only for English Ubuntu.

    and the whole option has been removed :(

    the `F2` key also avoids the mouse.

    @törzsmókus And French - *titre* ;)

  • 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.

  • 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.

  • 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"' 
    
  • 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"
    

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM