With Bash + iTerm2, how to name tabs?
Solution 1
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.
Solution 2
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.
Solution 3
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
Solution 4
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.
It requires node, but that is now a commonly installed platform. To install:
npm install -g iterm2-tab-set
Solution 5
Add this function to your ~/.bash_profile
file and it should work.
function title ()
{
TITLE=$*;
export PROMPT_COMMAND='echo -ne "\033]0;$TITLE\007"'
}
Related videos on Youtube
jay
Updated on September 18, 2022Comments
-
jay almost 2 years
I am pretty new to MFC,C++ programming, Please help me in this issue. In an edit box (created with CRichEditCtrl) there are several lines. I need to highlight one line(with red color) for some duration (example: highlight line 1 for 1 second and after that revert back the line color).
How can I do that? Thanks in advance
-
HikeMike about 12 yearspossible duplicate of Change iTerm2 window and tab titles in zsh
-
qazwsx about 12 yearsNot 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 about 12 yearsI.e. all my tabs have same host and user. So using those won't differentiate my tabs.
-
HikeMike about 12 yearsYou mean you want Show current job name from iTerm's preferences? Note that the linked topic isn't about username or host.
-
HikeMike about 12 yearsIt does if you replace
\e
by\033
. Also, you never indicated in your question what shell you are using. -
qazwsx about 12 yearsAfter issuing
$ echo -ne "\033]1;this is the title\a"
my tab title readsuser@host:~
just like before. I've unchecked everything under Preferences -> Appearance -> Window and Tab Titles. -
HikeMike about 12 yearsIs this the issue? superuser.com/questions/343747/…
-
qazwsx about 12 yearsPossibly, I have non-empty output for
$ echo $PROMPT_COMMAND
. -
VuVirt over 7 yearsYou can do it using EM_SETCHARFORMAT message: msdn.microsoft.com/en-us/library/windows/desktop/…
-
Admin about 2 yearsThis answer worked for me in iTerm2 3.4.15
-
-
qazwsx about 11 yearsI 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 thentitle dog
didn't turn tab title into "dog" -
luis over 10 years+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.
-
Stewie over 10 yearsThis works for a second until I issue a return on the tab that I have renamed.
-
Max Cantor about 10 years@Stewie In Preferences -> Profiles -> Terminal, uncheck "Allow terminal to report window title".
-
Stewie about 10 yearsIt is unchecked. It still renames the title.
-
dan zen over 9 years@Stewie, please check "Allow terminal to report window title", so the title will not be renamed by shell
-
Steve Bennett over 9 years@mawaldne, why does this work? It seems really bizarre - you start editing the theme name, then abandon?
-
MidnightLightning almost 9 years@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.
-
Ohonovksiy almost 9 yearsWhat if you have multiple panes open? Do you have to rename each one to fully name the tab itself?
-
SoEzPz over 8 yearsVery helpful. I wanted a pony; and I got one!
-
Dan over 8 years"Yuk with all those aliases." Enters answer no one can possible remember or type by hand.
-
Michael over 8 yearsNote that as-is, this will clobber iTerm shell integration.
-
C Johnson about 8 yearsI put mine in .bash_profile, restarted and it worked like a charm.
-
Taylor D. Edmiston over 7 yearsThis 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
. -
Emile Bergeron over 7 years@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
. -
Anand Bhararia over 7 years@EmileBergeron thx, although it seems you (we) are in a minority. The answer was only 1 line, the rest is for lazy people.
-
DavidPostill about 7 yearsPlease quote the essential parts of the answer from the reference link(s), as the answer can become invalid if the linked page(s) change.
-
Michael almost 7 yearsNote that in v3.1,
iterm2_preexec_invoke_cmd
has become__bp_precmd_invoke_cmd
-
Ashutosh Jindal about 6 yearsThis is awesome! I especially love the auto setting of the tab color. Thank you!
-
Sagar Jauhari almost 6 yearsworks for zsh also
-
Mikhail over 5 yearsThose 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 -
dmulvi about 5 yearsWow! This is a great tool!
-
qazwsx almost 5 yearsI 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>)
-
Mike Fogel almost 5 yearsthe 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"'
-
Fake Name almost 5 years
but that is now a commonly installed platform
Hahaha.ha...ha......, wait, are you serious? -
Jonathan Eunice almost 5 yearsYes, 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.
-
Danijel over 4 yearsWorks 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 over 4 yearsThis shows only current top directory:
export PROMPT_COMMAND='echo -ne "\033]0;${PWD##*/}\007"'
-
subelsky over 4 yearstabset works great, thanks for making it! this is a gamechanger for my workflow since I'm jumping between contexts so much
-
JESii over 4 yearsUnfortunately,
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. -
Jonathan Eunice over 4 yearsLatest 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.
-
SFbay007 about 4 yearsI don't see this option in iTerm2
-
Konstantin almost 4 yearsNeither 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 almost 4 yearsOh, well, I was editing the Session Name while what I had to be editing was the Tab Title. Now it works.
-
biesior almost 4 yearsMy upvote mainly for sample "pony" title :D
-
faximan over 2 yearsFor 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 )
-
Krishna Vedula over 2 yearsThis is the best solution. Simple straight forward and works on any shell. Love it
-
wviana over 2 yearsOk 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 addDISABLE_AUTO_TITLE=true
in your .zshrc . I fixed to me. -
Admin about 2 yearsDoesn't seem to be doing anything in modern (2022) iTerm2
-
Admin about 2 yearsiTerm2 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).
-
Admin about 2 yearsYou 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. -
Admin about 2 yearsLike 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. -
Admin about 2 yearsThis keyboard shortcut has stopped working for me :(