TransWikia.com

With Bash + iTerm2, how to name tabs?

Super User Asked on November 7, 2021

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?

16 Answers

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.

Answered by kryptonite30 on November 7, 2021

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

Answered by Bas on November 7, 2021

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!

Answered by yatsek on November 7, 2021

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.

Answered by Krishna Gupta on November 7, 2021

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

Max Cantor's comment worked for me.

Answered by B Seven on November 7, 2021

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 "33]0;${*}07"
  else
    test -e "${HOME}/.iterm2_shell_integration.bash" 
      && export PROMPT_COMMAND='echo -ne "33]0;${PWD/#$HOME/~}07";iterm2_preexec_invoke_cmd' 
      || export PROMPT_COMMAND='echo -ne "33]0;${PWD/#$HOME/~}07"'
  fi
}
title

Answered by user1318024 on November 7, 2021

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

Answered by lengxuehx on November 7, 2021

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

Answered by aianitro on November 7, 2021

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

Answered by Jonathan Eunice on November 7, 2021

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 "33]0;${*}07"
    else
        export PROMPT_COMMAND='echo -ne "33]0;${PWD/#$HOME/~}07";__bp_precmd_invoke_cmd'
    fi
}
title

Answered by Michael on November 7, 2021

I like this one:

#setup terminal tab title
function title {
    if [ "$1" ]
    then
        unset PROMPT_COMMAND
        echo -ne "33]0;${*}07"
    else
        export PROMPT_COMMAND='echo -ne "33]0;${PWD/#$HOME/~}07"'
    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

Answered by taylorstine on November 7, 2021

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 "33]0;$*07"
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.

Answered by Orwellophile on November 7, 2021

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

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

Answered by jiangyu7408 on November 7, 2021

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.

Answered by mawaldne on November 7, 2021

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

function title {
    echo -ne "33]0;"$*"07"
}

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

> title I want a pony!

And my tab gets titled accordingly.

Answered by Jason Sundram on November 7, 2021

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

export PROMPT_COMMAND='echo -ne "33]0;YOUR NAME HERE07"'

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 "33]0;${PWD/#$HOME/~}07"'

useful when you have 100 minimized terminals in your dock

hat tip to mac world

Answered by schpet on November 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP