85

None of the currently posted answers works/answers the question.

As per my original question, neither setting PS1 nor PROMPT_COMMAND had any effect.


Using only a command at the command prompt, how do I change the title of the current terminal tab?

Many posts suggest this:

echo -en "\033]0;New terminal title\a"

but it does nothing.

None of the current answers works (some don't answer the question), so for clarity:

  • Once the title is changed, I don't want it to change if I change directory etc
  • I don't want the same title on all tabs. I only want to set the title for the tab I run the command in
  • I want multiple tabs to each have different titles

Also, the PROMPT_COMMAND variable is not set in my terminal sessions. If I set it:

PROMPT_COMMAND='echo -en "\033]0;New terminal title\a"'

it has no effect.

What is the correct command?


FYI, the output of uname -a is:

Linux d136172 3.13.0-45-generic #74-Ubuntu SMP Tue Jan 13 19:36:28 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

Bohemian
  • 959

10 Answers10

48

from @Maythux, this one works for my needs to disregard my auto-prompt current-directory on terminal.

PROMPT_COMMAND='echo -en "\033]0;New terminal title\a"'

Instruction

Change the string on "New Terminal Name" with $("pwd"):

PROMPT_COMMAND='echo -en "\033]0; $("pwd") \a"'

This will automatically change the title even when you add a new tab.


I use the setting below which looks better, you can also play bash programming and set your own.

PROMPT_COMMAND='echo -en "\033]0;$(whoami)@$(hostname)|$(pwd|cut -d "/" -f 4-100)\a"'

Add this setting to your ~/.bashrc.

dessert
  • 40,956
Kelly DC
  • 589
37

From https://askubuntu.com/a/774543/455406, a bash-specific solution is to create a custom function (see e.g. this how-to) like

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

which allows you to call set-title <name you want to set it to>

WillC
  • 1,758
12

It is very likely that PROMPT_COMMAND is set and it is overwriting your choice of title every time the prompt is displayed. Try unsetting it and then issuing your title command:

PROMPT_COMMAND=
echo -en "\033]0;New terminal title\a"
John1024
  • 13,947
12

When the PS1 sets the title, any attempt to set the title using a command or PROMPT_COMMAND will fail, since the prompt is printed after all of them. For this reason, I prefer to keep a simple prompt while testing titles (PS1=$; unset PROMPT_COMMAND).

muru
  • 207,228
9

This thread may be a little old, but here is a solution that works for me:

https://blog.programster.org/ubuntu-16-04-set-terminal-title

Simply edit your $HOME/.bashrc file and add the following function:
set-title(){
ORIG=$PS1
TITLE="\e]2;$@\a"
PS1=${ORIG}${TITLE}
}

Now whenever you want to set the title of your terminal, just enter something like:
set-title "my awesome terminal title"

Sjoerd
  • 91
5

You can do it, either in CLI or GUI(I suppose you are using gnome-terminal, you can do for others just replace the name of app):

In CLI Run the command:

gconftool-2 --set /apps/gnome-terminal/profiles/Default/title --type=string "New Terminal Name"

Note: the new name is applied to all instances of terminal tabs, and not for the only current tab.

enter image description here

Or from GUI:

Go to Menu: Terminal --> Set Title --> Enter new title then save.


Now Why your command not work?

You should add this line to the .bashrc file and not directly to your terminal.

gedit .bashrc

Add this line:

PROMPT_COMMAND='echo -en "\033]0;New terminal title\a"'

Then save and source the bashrc file.

source .bashrc
Maythux
  • 87,123
4

Instructions

  1. Add settitle() to your .bashrc.
  2. source ~/.bashrc
  3. settitle Banana

settitle()

function settitle()
{
    if [ $# -eq 0 ]
        then
        eval set -- "\\u@\\h: \\w"
    fi

    case $TERM in
        xterm*) local title="\[\033]0;$@\007\]";;
        *) local title=''
    esac
    local prompt=$(echo "$PS1" | sed -e 's/\\\[\\033\]0;.*\\007\\\]//')
    PS1="${title}${prompt}"
}
earthmeLon
  • 11,658
3

Based on @muru answer

PS1 sets the title, any attempt to set the title using a command or PROMPT_COMMAND will fail, since the prompt is printed after all of them

This worked in my Elementary S.O :

PS1='\u:\W\$ '
PROMPT_COMMAND='echo -en "\033]0;New terminal title\a"'

I execute this in each new tab :

tab 1

tab 2

And as the previous image shows, I have several tabs with unique name.

16.04.1-Ubuntu

1

Using bash, wmctrl, xprop, ps

1) For a long-running active program:

For example, start a program (ranger) running in a terminal, started from the desktop, change the title, once, after some delay ( 5 seconds ) when the program starts :

startranger.sh:

#!/bin/bash
/usr/local/bin/changetitle.sh 5 ranger
/usr/local/bin/ranger

changetitle.sh:

#!/bin/bash
delay="$1"
shift
wintitle="$*"
winid=`xprop -root | grep _NET_ACTIVE_WINDOW | head -1 | awk '{print $5}' | sed 's/,//' | sed 's/^0x/0x0/'`
/bin/bash -c "sleep $delay; wmctrl -i -r $winid -N \"$wintitle\"" &

2) If you are running a terminal session without running an active program, update the title on a loop that ends when your terminal exits:

changetitleloop.sh 1 maintenance for server

running the above will update the title of the current terminal every 1 second even if you cd (can change it multiple times), using:

changetitleloop.sh

#!/bin/bash
interval="$1"
shift
wintitle="$*"
termpid="$(ps -p $$ -o ppid= | sed -e 's/^[ \t]*//')"
winid=`xprop -root | grep _NET_ACTIVE_WINDOW | head -1 | awk '{print $5}' | sed 's/,//' | sed 's/^0x/0x0/'`
/bin/bash -c "ss=\$$; echo \$ss > /tmp/term-$termpid.pid; while x=\$(wmctrl -i -r $winid -N \"$wintitle\"); ret=\$?; sleep $interval; owner=\$(cat /tmp/term-$termpid.pid); [ \$ret -eq 0 ] && [ \$ss -eq \$owner ]; do continue; done;" &
1

One solution may be to install the latest version of tmux.

tmux allows setting per-pane titles, enabled by this command:

tmux set -g pane-border-status top

They can also be displayed on the bottom.

Titles are then set via an escape sequence:

printf '\033]2│;%s\033\\' 'My Pane Title'

Each pane can have its own title and all titles show all the time.

The tmux panes will then look like this:

──0 "My Pane Title"──────┬──1 "Another Pane"───────
>                        │>

This was tested on linux mint 18.2 (like Ubuntu) with tmux 2.8. Installation was from a tarball.

If you want to be more productive in your terminal, tmux offers lots of other features too.

Mike Amy
  • 111