How to change Gnome-Terminal title?

Solution 1:

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

Solution 2:

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.

Solution 3:

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.

Solution 4:

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