With Bash + iTerm2, how to name tabs?

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?

10

16 Answers

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.

12

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.

10

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

3

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
9

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

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

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

1

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

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
1

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!

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

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

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

Max Cantor's comment worked for me.

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.

1

I created a script to set dirname and a custom color to an active iterm2 tab.

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.

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.

3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like