209

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?

nc4pk
  • 9,037
  • 14
  • 59
  • 71
qazwsx
  • 8,599
  • 24
  • 58
  • 86
  • 1
    possible duplicate of [Change iTerm2 window and tab titles in zsh](http://superuser.com/questions/292652/change-iterm2-window-and-tab-titles-in-zsh) – Daniel Beck May 02 '12 at 19:11
  • 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 May 02 '12 at 19:16
  • 1
    I.e. all my tabs have same host and user. So using those won't differentiate my tabs. – qazwsx May 02 '12 at 19:27
  • 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 May 02 '12 at 19:31
  • 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 May 02 '12 at 20:08
  • It does if you replace `\e` by `\033`. Also, you never indicated in your question what shell you are using. – Daniel Beck May 02 '12 at 20:25
  • 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 May 02 '12 at 20:30
  • Is this the issue? http://superuser.com/questions/343747/how-do-i-stop-automatic-changing-of-iterm-tab-titles/356088#356088 – Daniel Beck May 02 '12 at 20:31
  • Possibly, I have non-empty output for `$ echo $PROMPT_COMMAND`. – qazwsx May 02 '12 at 20:34
  • [This answer](https://superuser.com/a/292660/5200) worked for me in iTerm2 3.4.15 – jcollum Apr 27 '22 at 16:31

17 Answers17

251

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.

SomeGuyOnAComputer
  • 537
  • 1
  • 5
  • 12
mawaldne
  • 2,636
  • 1
  • 13
  • 6
  • 11
    This works for a second until I issue a return on the tab that I have renamed. – Stewie Jan 29 '14 at 15:39
  • 17
    @Stewie In Preferences -> Profiles -> Terminal, uncheck "Allow terminal to report window title". – Max Cantor Apr 04 '14 at 13:19
  • 3
    It is unchecked. It still renames the title. – Stewie Apr 06 '14 at 01:05
  • 2
    @Stewie, please check "Allow terminal to report window title", so the title will not be renamed by shell – dan zen Oct 08 '14 at 01:49
  • @mawaldne, why does this work? It seems really bizarre - you start editing the theme name, then abandon? – Steve Bennett Mar 22 '15 at 23:48
  • 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 Aug 04 '15 at 13:39
  • 4
    What if you have multiple panes open? Do you have to rename each one to fully name the tab itself? – theicfire Aug 10 '15 at 21:01
  • 2
    This 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 D. Edmiston Nov 29 '16 at 18:50
  • 1
    Neither of the solutions seem to work with the current version. Entering a new command removes the set name again. None of the settings mentioned in the comments seem to exist in the current version. – Konstantin Aug 06 '20 at 10:03
  • Oh, well, I was editing the Session Name while what I had to be editing was the Tab Title. Now it works. – Konstantin Aug 06 '20 at 10:07
  • You lose the tab's title after hitting "return" on the tab because some part of your shell is actively trying to change the tab title for you! If you're using oh-my-zsh, it has this functionality built in. Something is hooked on your shell precmd/preexec so that it's constantly updating it. The proper solution is to disable whatever is trying to rename your tab. With oh-my-zsh, just set the env `DISABLE_AUTO_TITLE=true` and it'll stop. – Chris Apr 30 '22 at 02:56
  • This keyboard shortcut has stopped working for me :( – TheLukeMcCarthy May 16 '22 at 01:27
138

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
  • 3,251
  • 7
  • 26
  • 30
  • 1
    I 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 Jun 06 '13 at 21:06
  • 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 Dec 10 '13 at 19:52
  • 4
    Very helpful. I wanted a pony; and I got one! – SoEzPz Oct 26 '15 at 16:55
  • 1
    I put mine in .bash_profile, restarted and it worked like a charm. – C.J. Apr 14 '16 at 20:33
  • works for zsh also – Sagar Jauhari Jul 19 '18 at 23:17
  • 3
    Those 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 Oct 26 '18 at 21:55
  • My upvote mainly for sample "pony" title :D – biesior Sep 25 '20 at 05:39
  • 2
    For anyone reading this in 2021, I also needed to check iTerm -> Preferences -> Profiles -> General -> Applications in terminal may change the title. (v 3.4.10 ) – faximan Nov 16 '21 at 14:32
  • 1
    Ok folks, just figured out why mine was undoing my title change after some time, as @Mikhail mention, if you want to check if is something else setting your title try the `echo -ne "\033]0;"Hello World"\007" && sleep 3` If it changes to "Hello World" for 3 seconds than it changes back it may be oh-my-zsh auto_title feature. To disable it add `DISABLE_AUTO_TITLE=true` in your .zshrc . I fixed to me. – wviana Mar 15 '22 at 13:26
  • Like wviana said, if you're using oh-my-zsh, you'll need to set `DISABLE_AUTO_TITLE=true` to disable oh-my-zsh's built in tab title changing functionality. But this only applies to omz users. If you have the same problem and aren't using omz you'll need to figure out what's causing it. – Chris Apr 30 '22 at 02:58
53

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
  • 1,099
  • 9
  • 8
  • 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 Aug 09 '19 at 13:46
  • 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 Nov 07 '19 at 07:27
  • This shows only current top directory: `export PROMPT_COMMAND='echo -ne "\033]0;${PWD##*/}\007"'` – Danijel Nov 07 '19 at 07:41
29

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
  • 391
  • 3
  • 5
  • This is awesome! I especially love the auto setting of the tab color. Thank you! – Ashutosh Jindal Jun 11 '18 at 14:22
  • 1
    Wow! This is a great tool! – dmulvi Apr 05 '19 at 18:00
  • I currently use this method instead of the one described in the accepted answer (CmdI, type something, and hit ESC) – qazwsx Jul 24 '19 at 18:52
  • 1
    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 Aug 14 '19 at 06:19
  • tabset works great, thanks for making it! this is a gamechanger for my workflow since I'm jumping between contexts so much – subelsky Jan 23 '20 at 17:45
  • 1
    Unfortunately, `title` option doesn't seem to work with latest iTerm2. Little or no response to issues. If all you want is the color option, then go for it. – JESii Mar 14 '20 at 14:19
  • Latest versions of iTerm2, perhaps based on changes in macOS, no longer work as they once did. I occasionally check on the iTerm2 project hoping they've fixed or worked around the breakage, but so far, no joy. – Jonathan Eunice Mar 16 '20 at 15:00
  • This is the best solution. Simple straight forward and works on any shell. Love it – Krishna Vedula Dec 16 '21 at 04:51
  • iTerm2 3.4.15: I get the transparent text on the right but don't get the same title on the tab itself (at the top). – jcollum Apr 27 '22 at 16:22
  • It's great to set the color of the tab, but the text overlay is really slow. – Martin Braun Jan 23 '23 at 22:10
17

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

function title ()
{
    TITLE=$*;
    export PROMPT_COMMAND='echo -ne "\033]0;$TITLE\007"'
}
richsage
  • 113
  • 5
jiangyu7408
  • 171
  • 1
  • 2
8

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
  • 181
  • 1
  • 2
  • 1
    Note that as-is, this will clobber [iTerm shell integration.](https://iterm2.com/shell_integration.html) – Michael Mar 18 '16 at 15:30
7

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
  • 999
  • 10
  • 16
5

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
Toby Speight
  • 4,866
  • 1
  • 26
  • 36
user1318024
  • 151
  • 1
  • 2
5

I was looking for solution which works on vanilla iTerm2. One quite nicely working which I've found is to add keyboard shortcut which will execute this option (available either by double clicking tab or by choosing from menu Window → Edit TabTitle)

To do so:

  • open Preferences - Cmd+, or menu iTerm2 → Preferences
  • go to Keys submenu and click + at the bottom
  • record your preferred key stroke (like Super+Ctrl+Shift+e) and choose "Action" Select Menu Item
  • choose Edit Tab Title from list of available positions
  • voila!
yatsek
  • 631
  • 7
  • 7
3

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

MJH
  • 1,115
  • 4
  • 13
  • 20
aianitro
  • 131
  • 3
2

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
  • 121
  • 4
  • 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 May 01 '17 at 09:28
2
Preferences -> Profiles -> Terminal
  uncheck Terminal may set Tab/Window title

Max Cantor's comment worked for me.

B Seven
  • 370
  • 1
  • 6
  • 18
2

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.

1

I created a script to set dirname and a custom color to an active iterm2 tab. https://gist.github.com/bastoker/95d2f1d7c1354cb6b888363103a90645

Bas
  • 111
  • 3
1

If you're using multiple panes(tmux) and you want to rename all of them (the tab title will still change if you're active in a different pane), you can press Option + Command + i to activate multi-pane input on all panes on your current tab and then press Command + i to rename all panes to the name you want. That way, no matter which pane is active, your tab will have the same name.

0

Other answers provide no consistent way to override the title. I.e. once you enter nvim the title will be lost. If you already know how your workspace will be, please take a look at itomate. It allows you to spawn several tabs and panes with persistent tab names. You can even run commands automatically to get things going quickly.

What you do is build an itomate.yaml with contents like this one:

version: "1.0"
profile: "Default"
tabs:
  admin:
    root: "$HOME/proj/test/server"
    title: "admin"
    panes:
      - position: "1/1"
        commands:
          - "tabset --color #ff0000"
          - "nvim"
      - position: "1/2"
        commands:
          - "tabset --color #ff0000"
          - "nvm use"
          - "npm start"
  customer:
    root: "$HOME/proj/test/client1"
    title: "customer"
    panes:
      - position: "1/1"
        commands:
          - "tabset --color #ffcc00"
          - "nvim"
  driver:
    root: "$HOME/proj/test/client2"
    title: "driver"
    panes:
      - position: "1/1"
        commands:
          - "tabset --color #00ffcc"
          - "nvim"

and then you just need to run itomate to spawn your windows and tabs with the right names. itomate will set the titles in a way that not even vim or any other applications can change them.

You can see in the example, I also use tabset by Jason Sundram to modify the color of the tab. I want to advice against using tabset to set the name of the tab itself. It's rendered overlay make switching tabs slower and the set titles also don't persist when using other applications in your session.

Martin Braun
  • 681
  • 2
  • 10
  • 22
-1

Yuk, all those aliases and functions. Easier solution (if you are root), paste this into a terminal. This will create a 1 line bash script and put it in the path.

You can then change the title at any time using:

title "New title"
TARGET=/usr/local/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
  • 499
  • 5
  • 9
  • 12
    "Yuk with all those aliases." *Enters answer no one can possible remember or type by hand.* – Dan Jan 14 '16 at 18:09
  • 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 Jan 09 '17 at 21:43
  • @EmileBergeron thx, although it seems you (we) are in a minority. The answer was only 1 line, the rest is for lazy people. – Orwellophile Jan 10 '17 at 07:09