38

This question already has an answer here:

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

marked as duplicate by WinEunuuchs2Unix, karel, dessert command-line Aug 11 at 6:34

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    Do you mean tab (mentioned in the question) or window (the title). One is more complicated than the other :) related: askubuntu.com/questions/626505/… – Jacob Vlijm Jun 16 '15 at 6:36
  • 2
    Which terminal program? What does $PS1 contain? – muru Jun 22 '15 at 0:15
  • 2
    @muru terminal program is /usr/bin/gnome-terminal (from standard install). echo $PS1 -> \[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ – Bohemian Jun 22 '15 at 0:25
  • 2
    @Bohemian As you can see, your PS1 also sets the title – muru Jun 22 '15 at 0:25
  • 1
    I don't want to take credit for it, but I found a really nice way of changing the tab name located here: blog.programster.org/ubuntu-16-04-set-terminal-title I implemented it in my bash profile script and now when I open a terminal I just type: set-title "TabName" and it changes. The only problem is that the main window name changes as well. Try it and you will see what I mean. Hope this helps! – Shanemeister Apr 19 '18 at 19:51

10 Answers 10

21

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.

11

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>

  • how run this function in a custom commands please I fail to do it – Webwoman Sep 17 '18 at 14:43
  • per the how-to I referenced "create its own executable script in ~/bin/ which won't exist by default (it's just a directory) but should be in your path. Remember for this the file will need to be executable (chmod +x filename) and start with a proper #!/bin/bash stanza." (1) create a file in ~/bin (2) paste/type the code into the file (3) save the file (4) chmod the file to be executable . Then if you saved it as '~/bin/setATitle' you should be able to run $ setATitle a title – WillC Sep 24 '18 at 6:48
  • 1
    Works in Ubuntu 18.04, thank you. (restart Terminal after you add the script to .bashrc) – user1692094 Dec 21 '18 at 14:33
  • +1. You can also add it to ~/.bash_aliases then export it with export -f set-title. After sourceing ~/.bash_aliases, you can now do set-title <title> for each tab. – Gino Mempin Jun 12 at 0:20
  • I use a coloured prompt along with changing the title. I was having history issues on long commands until I though to escape the title along with my prompt - just like you did with \[ and \] - thanks – myol Jul 31 at 16:23
10

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

5

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"
  • 1
    env | grep PROMPT_COMMAND returns nothing. – Bohemian Jun 16 '15 at 3:37
  • @Bohemian Why would it? It is a variable not an environment variable. If you want to check if it is set, use echo $PROMP_COMMAND – Anthon Jun 16 '15 at 4:15
  • @Anthon echo $PROMP_COMMAND prints blank. And although your "why would it?" was intended as rhetorical, I'll answer it: env prints all variables, that's why. Prove it yourself with this simple test: export foo=bar; env | grep foo prints foo=bar – Bohemian Jun 16 '15 at 14:43
  • 1
    @Bohemian PROMPT_COMMAND=something and export PROMPT_COMMAND=something will have the same effect on the current session. – John1024 Jun 17 '15 at 0:12
  • 1
    @John1024 yes, in a fresh tab echo $PROMPT_COMMAND prints nothing. After executing PROMPT_COMMAND='echo -en "\033]0;New terminal title\a"', executing echo $PROMPT_COMMAND prints as expected, but the title of the terminal tab is unchanged (and remains so after other commands) – Bohemian Jun 17 '15 at 7:03
3

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
  • 4
    But I don't want to set the title to the same title for every tab; I want to have every tab have its own different title (depending on what use I've put it to, so I can easily find the tab I want) – Bohemian Jun 16 '15 at 15:11
  • I think what you need is simply complicated enough to get your answer – Maythux Jun 17 '15 at 5:56
3

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"

2

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}"
}
  • Thanks, but does nothing. – Bohemian Jun 16 '15 at 3:56
  • does NOT work in xfce4-terminal 0.8.3 – hanshenrik Mar 24 '17 at 19:00
  • 2
    what does the eval set -- "\\u@\\h: \\w do ? – Ciprian Tomoiagă Apr 24 '17 at 11:01
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

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

  • This worked for me, using Xfce terminal. – Smile4ever Apr 20 '18 at 19:27
0

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.