79

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
  • 899
  • 1
  • 7
  • 9
  • 2
    Do you mean tab (mentioned in the question) or window (the title). One is more complicated than the other :) related: http://askubuntu.com/questions/626505/how-do-i-permanently-change-window-titles/626524#626524 – Jacob Vlijm Jun 16 '15 at 06:36
  • @JacobVlijm I mean tab. I didn't realise there was a difference. – Bohemian Jun 16 '15 at 08:31
  • 3
    Which terminal program? What does $PS1 contain? – muru Jun 22 '15 at 00:15
  • 3
    @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 00:25
  • 2
    @Bohemian As you can see, your PS1 also sets the title – muru Jun 22 '15 at 00:25
  • @muru Yes, so that's the answer then. Would you like to post it, so I can accept it? Perhaps noting that PS1 "overrides" PROMPT_COMMAND (if that's true) – Bohemian Jun 22 '15 at 00:26
  • @Bohemian The PROMPT_COMMAND is executed before the prompt, that's by design. But now that I have posted an answer, I wonder if I should have. It doesn't actually answer your question, and is more a note for others. I suggest you accept any of the other answers that work, and upvote mine. – muru Jun 22 '15 at 00:38
  • echo -ne "\033]2;test change title\007" From here https://stackoverflow.com/questions/19897787/change-konsole-tab-title-from-command-line-and-make-it-persistent – John Aug 25 '17 at 16:50
  • @John as per question, that does nothing – Bohemian Aug 25 '17 at 21:59
  • 6
    I don't want to take credit for it, but I found a really nice way of changing the tab name located here: https://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
  • what do you get when you echo $PROMPT_COMMAND? – JBallin Jun 13 '18 at 22:43

10 Answers10

42

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
  • 39,982
Kelly DC
  • 521
  • 1
    Yes but this is executed before bash displays the command. So if you type python python will not appear in the title until you exit from python again. That is probably not what you want. – Nils Dec 05 '19 at 12:39
  • thanks for this! but how do I set PROMPT_COMMAND='echo -en "\033]0;$(whoami)@$(hostname)|$(pwd|cut -d "/" -f 4-100)\a"' as a bash alias? being a bash newbie, I cannot get the quotation marks syntax right, could you please help? – alfx Apr 11 '23 at 15:14
35

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,718
  • 1
    how run this function in a custom commands please I fail to do it – DiaJos Sep 17 '18 at 14:43
  • 1
    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 06:48
  • 2
    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 '19 at 00: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 '19 at 16:23
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,687
  • 43
  • 51
  • 1
    env | grep PROMPT_COMMAND returns nothing. – Bohemian Jun 16 '15 at 03:37
  • 1
    @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 04: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
  • Maybe you should try: foo=bar; env | grep foo, as that prints nothing, and then do echo $foo. You'll see the difference between setting a variable in the shell, and a setting a variable and exporting it into the environment. – Anthon Jun 16 '15 at 15:00
  • @Anthon is there an effective difference? Isn't setting a shell variable vs exporting it the same thing to the app using the variable? – Bohemian Jun 16 '15 at 15:16
  • Yes there is an effective difference, as it is not the same because PROMPT_COMMAND is used by bash itself if it is set as a variable, exported or not. It doesn't have to be visible in the environment for bash itself to see it and use it. That is why checking if PROMPT_COMMAND is in the environment does not help to see "if it is there" for bash to use – Anthon Jun 16 '15 at 15:33
  • 1
    @Bohemian (1) Note that echo $PROMP_COMMAND which appeared in this comment thread contains a typo. Please try instead echo $PROMPT_COMMAND. (2) Also, note that env is an external program, not a shell built-in. Consequently, it only sees exported variables. env | grep PROMPT_COMMAND returns nothing on my computer even though PROMPT_COMMAND is assigned and used to set the title. – John1024 Jun 16 '15 at 16:59
  • @anthon thanks for the answer, but you haven't actually answered my "is it the same" question. Let me rephrase it: if the setting does not exist but it is exported will it still work? – Bohemian Jun 16 '15 at 20:32
  • @Bohemian Yes. It is not necessary for bash to be able to work with this variable, for it to be be exported. (The exporting is for program started from bash) From man bash: *PROMPT_COMMAND If set, the value is executed as a command prior to issuing each primary prompt.. The keyword there is set* it doesn't say set and exported. – Anthon Jun 16 '15 at 21:37
  • @anthon again thanks, sorry to be a pain, but you still haven't answered the question. Let me rephrase again. If PROMPT_COMMAND is not set and I execute export PROMPT_COMMAND=somecmd, will that take effect in the current terminal session? In other words, can I do either? Or is it that setting works for the current session and exported settings only take effect (are passed to) programs started by the current session? – Bohemian Jun 16 '15 at 23:10
  • 1
    @Bohemian PROMPT_COMMAND=something and export PROMPT_COMMAND=something will have the same effect on the current session. – John1024 Jun 17 '15 at 00:12
  • @John1024 that's what I thought and what I'e been trying to get across to Anthon. Thanks for clearing that up. – Bohemian Jun 17 '15 at 00:29
  • @John1024 Also, more importantly, executing your command has no effect on the title on the tab – Bohemian Jun 17 '15 at 00:33
  • @Bohemian I am not sure what you can misread about my answer "Yes" to your question "If the setting does not exists but is exported will it still work?" – Anthon Jun 17 '15 at 04:42
  • But I recommend that you read up on what export does to a variable or to a variable assignment, because your question and misinterpretation of my answers expose that you don't grasp what it does (and I that I cannot come up with a good example that fits in this commentbox restriction to explain it) – Anthon Jun 17 '15 at 04:47
  • @Bohemian OK. Just to close the loop, when you open a new terminal does echo $PROMPT_COMMAND (as opposed to echo $PROMP_COMMAND) print nothing? – John1024 Jun 17 '15 at 06:10
  • 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 07:03
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
  • 197,895
  • 55
  • 485
  • 740
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
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,247
4

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
  • 84,289
  • 7
    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 05:56
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;" &
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.

Mike Amy
  • 101