With Bash + iTerm2, how to name tabs?

185

58

In iTerm2 (Build 1.0.0.20120203), I typically open several tabs, each of which has split panes , and is about one particular theme of work, for example revision control, coding, managing files, mysql terminal work. I typically need to switch between 5 or more tabs in my work flow. It is sometimes hard to remember or tell which is which by looking at the content of the screen. I'd like to name the tabs somehow, so I can quickly tell which is which by quickly glancing. Is this possible?

qazwsx

Posted 2012-05-02T19:08:06.097

Reputation: 6 739

1

possible duplicate of Change iTerm2 window and tab titles in zsh

– Daniel Beck – 2012-05-02T19:11:29.753

Not entirely duplicate. So how to add the currently running app as a part of tab title? I.e. which tab is running emacs, mysql, etc.? – qazwsx – 2012-05-02T19:16:53.980

1I.e. all my tabs have same host and user. So using those won't differentiate my tabs. – qazwsx – 2012-05-02T19:27:57.210

You mean you want Show current job name from iTerm's preferences? Note that the linked topic isn't about username or host. – Daniel Beck – 2012-05-02T19:31:10.180

Right, I want to show some indication of what program is running or was run in each tabs. Also, the solution given in the other post doesn't work for Bash + iTerm2. – qazwsx – 2012-05-02T20:08:07.690

It does if you replace \e by \033. Also, you never indicated in your question what shell you are using. – Daniel Beck – 2012-05-02T20:25:04.840

After issuing $ echo -ne "\033]1;this is the title\a" my tab title reads user@host:~ just like before. I've unchecked everything under Preferences -> Appearance -> Window and Tab Titles. – qazwsx – 2012-05-02T20:30:25.993

Possibly, I have non-empty output for $ echo $PROMPT_COMMAND. – qazwsx – 2012-05-02T20:34:36.050

Answers

218

Since you're using iterm2 on a mac, another option is you can just hit CmdI, type something, and hit ESC.

The terminal solution is a bit quicker than this, but just wanted to let you know.

mawaldne

Posted 2012-05-02T19:08:06.097

Reputation: 2 306

2@Stewie, please check "Allow terminal to report window title", so the title will not be renamed by shell – dan zen – 2014-10-08T01:49:05.830

@mawaldne, why does this work? It seems really bizarre - you start editing the theme name, then abandon? – Steve Bennett – 2015-03-22T23:48:22.157

6@SteveBennett it's not the "theme" (actually the term is "profile") itself; the CMD+I command is "Edit Current Session..." (under the View menu), so it's just changing that tab's instance of the profile. Hitting Escape just closes the window, which is needed since that window doesn't have a "save" button on it. – MidnightLightning – 2015-08-04T13:39:37.797

4What if you have multiple panes open? Do you have to rename each one to fully name the tab itself? – theicfire – 2015-08-10T21:01:32.960

2This doesn't seem to work to save the name on the profile in iTerm 2 3.0.12. What worked for me was iTerm > Preferences > Appearance > Window & Tab Titles > Show profile name. – Taylor Edmiston – 2016-11-29T18:50:32.360

11This works for a second until I issue a return on the tab that I have renamed. – Stewie – 2014-01-29T15:39:50.943

15@Stewie In Preferences -> Profiles -> Terminal, uncheck "Allow terminal to report window title". – Max Cantor – 2014-04-04T13:19:04.703

3It is unchecked. It still renames the title. – Stewie – 2014-04-06T01:05:16.220

122

I've found the following function, placed in my ~/.bashrc to be helpful:

function title {
    echo -ne "\033]0;"$*"\007"
}

Then I can call it from my bash prompt as follows:

> title I want a pony!

And my tab gets titled accordingly.

Jason Sundram

Posted 2012-05-02T19:08:06.097

Reputation: 2 946

3Very helpful. I wanted a pony; and I got one! – SoEzPz – 2015-10-26T16:55:41.293

1I put mine in .bash_profile, restarted and it worked like a charm. – C Johnson – 2016-04-14T20:33:29.460

works for zsh also – Sagar Jauhari – 2018-07-19T23:17:13.187

1Those thinking it doesn't work it's because your command prompt instantly changes it back. Try title dog && sleep 5 and you'll see that it works. Note the answer from @schpet to address this – Mikhail – 2018-10-26T21:55:29.530

1I tried this, but it still doesn't work. I put this definition into bash_aliases, and have it loaded in .profile (if [ -f ${HOME}/.bash_aliases ]; then . ${HOME}/.bash_aliases fi) But then title dog didn't turn tab title into "dog" – qazwsx – 2013-06-06T21:06:01.847

2+1 - I added mine to /etc/profile just cause that's where my aliases are... don't forget to source the file after you're done. Note: this also works in terminal. – blak3r – 2013-12-10T19:52:39.970

48

run this command to set the title of your tab or window:

export PROMPT_COMMAND='echo -ne "\033]0;YOUR NAME HERE\007"'

i've added the following to my ~/.bash_profile to always list the current directory relative to my home dir:

export PROMPT_COMMAND='echo -ne "\033]0;${PWD/#$HOME/~}\007"'

useful when you have 100 minimized terminals in your dock

hat tip to mac world

schpet

Posted 2012-05-02T19:08:06.097

Reputation: 909

the escaping on that export command didn't quite work on my bash for some reason (v5.0.7), here's what worked for me: export PROMPT_COMMAND='echo -ne "\033]0;${PWD/$HOME/\~}\007"' – Mike Fogel – 2019-08-09T13:46:06.910

Works nicely. I'd love to have only last directory in path listed instead of the full path - full path gets way too long, and therefore gets abbreviated with ..., so not visible when many tabs are in place. – Danijel – 2019-11-07T07:27:41.407

This shows only current top directory: export PROMPT_COMMAND='echo -ne "\033]0;${PWD##*/}\007"' – Danijel – 2019-11-07T07:41:08.127

20

I used solutions similar to the above for quite a while, but I use enough tabs that I also want them color-coded for easy visual reference. So I whipped up tabset, a utility to set the tab title, badge, and color based on the kind of work I am doing in each tab.

example

It requires node, but that is now a commonly installed platform. To install:

npm install -g iterm2-tab-set

Jonathan Eunice

Posted 2012-05-02T19:08:06.097

Reputation: 301

This is awesome! I especially love the auto setting of the tab color. Thank you! – Ashutosh Jindal – 2018-06-11T14:22:46.953

1Wow! This is a great tool! – dmulvi – 2019-04-05T18:00:43.137

I currently use this method instead of the one described in the accepted answer (<kbd>Cmd</kbd><kbd>I</kbd>, type something, and hit <kbd>ESC</kbd>) – qazwsx – 2019-07-24T18:52:57.573

but that is now a commonly installed platform Hahaha.ha...ha......, wait, are you serious? – Fake Name – 2019-08-14T03:54:01.620

Yes, serious. Developers and DevOps people—these days, the natural target audience of terminal applications—have node.js installed. Or can install it easily. Those who don't/can't are welcome to fall back to bash/zsh/etc.-based approaches. – Jonathan Eunice – 2019-08-14T06:19:30.253

tabset works great, thanks for making it! this is a gamechanger for my workflow since I'm jumping between contexts so much – subelsky – 2020-01-23T17:45:28.117

16

Add this function to your ~/.bash_profile file and it should work.

function title ()
{
    TITLE=$*;
    export PROMPT_COMMAND='echo -ne "\033]0;$TITLE\007"'
}

jiangyu7408

Posted 2012-05-02T19:08:06.097

Reputation: 161

7

I like this one:

#setup terminal tab title
function title {
    if [ "$1" ]
    then
        unset PROMPT_COMMAND
        echo -ne "\033]0;${*}\007"
    else
        export PROMPT_COMMAND='echo -ne "\033]0;${PWD/#$HOME/~}\007"'
    fi
}
title

It will let you toggle the name of a tab between a custom name and a default of your CWD.

title -> your tab title will be ~/YOUR_CWD/

title hey there -> your tab title will be hey there

taylorstine

Posted 2012-05-02T19:08:06.097

Reputation: 171

1

Note that as-is, this will clobber iTerm shell integration.

– Michael - Where's Clay Shirky – 2016-03-18T15:30:59.747

5

I really like taylorstine's answer, but it breaks iTerm2's shell integration which relies on the PROMPT_COMMAND variable. You can modify Taylor's code to correct this by adding the __bp_precmd_invoke_cmd back into the PROMPT_COMMAND any time you tinker with it:

# iTerm2 shell integration
test -e "${HOME}/.iterm2_shell_integration.bash" && source "${HOME}/.iterm2_shell_integration.bash"

# iTerm2 tab titles
function title {
    if [ "$1" ]
    then
        export PROMPT_COMMAND='__bp_precmd_invoke_cmd'
        echo -ne "\033]0;${*}\007"
    else
        export PROMPT_COMMAND='echo -ne "\033]0;${PWD/#$HOME/\~}\007";__bp_precmd_invoke_cmd'
    fi
}
title

Michael - Where's Clay Shirky

Posted 2012-05-02T19:08:06.097

Reputation: 491

3

I like Michael's answer.

But what if .iterm2_shell_integration.bash does not exist?

Here's my take:

# iTerm2 shell integration
test -e "${HOME}/.iterm2_shell_integration.bash" && source "${HOME}/.iterm2_shell_integration.bash"

# iTerm2 tab titles
function title {
  if [ "$1" ] ; then
    test -e "${HOME}/.iterm2_shell_integration.bash" \
      && export PROMPT_COMMAND='iterm2_preexec_invoke_cmd' \
      || unset PROMPT_COMMAND
    echo -ne "\033]0;${*}\007"
  else
    test -e "${HOME}/.iterm2_shell_integration.bash" \
      && export PROMPT_COMMAND='echo -ne "\033]0;${PWD/#$HOME/~}\007";iterm2_preexec_invoke_cmd' \
      || export PROMPT_COMMAND='echo -ne "\033]0;${PWD/#$HOME/~}\007"'
  fi
}
title

user1318024

Posted 2012-05-02T19:08:06.097

Reputation: 31

1Note that in v3.1, iterm2_preexec_invoke_cmd has become __bp_precmd_invoke_cmd – Michael - Where's Clay Shirky – 2017-09-20T21:50:31.303

2

If you're working with Profiles (which is very convenient): Preferences -> Appearance -> Window & Tab Titles: tick 'Show profile name':

image

That's how it looks after:

thumbnail linked to main image

aianitro

Posted 2012-05-02T19:08:06.097

Reputation: 121

2

Preferences -> Profiles -> Terminal
  uncheck Terminal may set Tab/Window title

Max Cantor's comment worked for me.

B Seven

Posted 2012-05-02T19:08:06.097

Reputation: 370

1

I think Automatic Profile Switching and Badges are exactly designed for what you need:

Automatic Profile Switching iTerm2 can use information it knows about your current path, host name, and user name to change profiles. For example, your window's background color or the terminal's character encoding could change when connecting to different hosts.

Badges You can put a badge in the top right of your terminal showing information about the current session. It can show your username, hostname, or even custom data like the current git branch.

so the result may like this:

enter image description here

lengxuehx

Posted 2012-05-02T19:08:06.097

Reputation: 111

Please quote the essential parts of the answer from the reference link(s), as the answer can become invalid if the linked page(s) change. – DavidPostill – 2017-05-01T09:28:28.997

0

I would like to extend B Seven's answer a little for absolute clarity.

Since most of us would like to know how can one set a title of a tab even when they are not in local shell, instead of in remote shell (e.g over ssh).

Step 1. Preferences -> Profiles -> Terminal uncheck Terminal may set Tab/Window title

Step 2. For each tabs, double click on the tab -> Session Title

Now, whatever you'd set in the session title, it would stay as is.

Krishna Gupta

Posted 2012-05-02T19:08:06.097

Reputation: 111

-2

Yuk, all those aliases and functions. Easier solution (if you are root), paste this into a terminal:

TARGET=/usr/bin/title
sudo tee "$TARGET" <<'EOF'
#!/usr/bin/env bash
echo -ne "\033]0;$*\007"
EOF
sudo chmod 755 "$TARGET"

Or just make a file call title somewhere in your path, or global path, and paste the two lines between EOF.

Orwellophile

Posted 2012-05-02T19:08:06.097

Reputation: 451

12"Yuk with all those aliases." Enters answer no one can possible remember or type by hand. – Dan – 2016-01-14T18:09:34.683

1@Dan not saying this is great, just that there's no need to remember that since it's just creating a script called title in /usr/bin. – Emile Bergeron – 2017-01-09T21:43:36.060

@EmileBergeron thx, although it seems you (we) are in a minority. The answer was only 1 line, the rest is for lazy people. – Orwellophile – 2017-01-10T07:09:50.483