With Bash + iTerm2, how to name tabs?

376

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

hat tip to mac world

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.

example

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"'
}
Share:
376

Related videos on Youtube

jay
Author by

jay

Updated on September 18, 2022

Comments

  • jay
    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
      HikeMike about 12 years
    • qazwsx
      qazwsx about 12 years
      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
      qazwsx about 12 years
      I.e. all my tabs have same host and user. So using those won't differentiate my tabs.
    • HikeMike
      HikeMike about 12 years
      You mean you want Show current job name from iTerm's preferences? Note that the linked topic isn't about username or host.
    • HikeMike
      HikeMike about 12 years
      It does if you replace \e by \033. Also, you never indicated in your question what shell you are using.
    • qazwsx
      qazwsx about 12 years
      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.
    • HikeMike
      HikeMike about 12 years
    • qazwsx
      qazwsx about 12 years
      Possibly, I have non-empty output for $ echo $PROMPT_COMMAND.
    • VuVirt
      VuVirt over 7 years
      You can do it using EM_SETCHARFORMAT message: msdn.microsoft.com/en-us/library/windows/desktop/…
    • Admin
      Admin about 2 years
      This answer worked for me in iTerm2 3.4.15
  • qazwsx
    qazwsx about 11 years
    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"
  • luis
    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
    Stewie over 10 years
    This works for a second until I issue a return on the tab that I have renamed.
  • Max Cantor
    Max Cantor about 10 years
    @Stewie In Preferences -> Profiles -> Terminal, uncheck "Allow terminal to report window title".
  • Stewie
    Stewie about 10 years
    It is unchecked. It still renames the title.
  • dan zen
    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
    Steve Bennett over 9 years
    @mawaldne, why does this work? It seems really bizarre - you start editing the theme name, then abandon?
  • MidnightLightning
    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
    Ohonovksiy almost 9 years
    What if you have multiple panes open? Do you have to rename each one to fully name the tab itself?
  • SoEzPz
    SoEzPz over 8 years
    Very helpful. I wanted a pony; and I got one!
  • Dan
    Dan over 8 years
    "Yuk with all those aliases." Enters answer no one can possible remember or type by hand.
  • Michael
    Michael over 8 years
    Note that as-is, this will clobber iTerm shell integration.
  • C Johnson
    C Johnson about 8 years
    I put mine in .bash_profile, restarted and it worked like a charm.
  • Taylor D. Edmiston
    Taylor D. Edmiston over 7 years
    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.
  • Emile Bergeron
    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
    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
    DavidPostill about 7 years
    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.
  • Michael
    Michael almost 7 years
    Note that in v3.1, iterm2_preexec_invoke_cmd has become __bp_precmd_invoke_cmd
  • Ashutosh Jindal
    Ashutosh Jindal about 6 years
    This is awesome! I especially love the auto setting of the tab color. Thank you!
  • Sagar Jauhari
    Sagar Jauhari almost 6 years
    works for zsh also
  • Mikhail
    Mikhail over 5 years
    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
  • dmulvi
    dmulvi about 5 years
    Wow! This is a great tool!
  • qazwsx
    qazwsx almost 5 years
    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>)
  • Mike Fogel
    Mike Fogel almost 5 years
    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"'
  • Fake Name
    Fake Name almost 5 years
    but that is now a commonly installed platform Hahaha.ha...ha......, wait, are you serious?
  • Jonathan Eunice
    Jonathan Eunice almost 5 years
    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.
  • Danijel
    Danijel over 4 years
    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
    Danijel over 4 years
    This shows only current top directory: export PROMPT_COMMAND='echo -ne "\033]0;${PWD##*/}\007"'
  • subelsky
    subelsky over 4 years
    tabset works great, thanks for making it! this is a gamechanger for my workflow since I'm jumping between contexts so much
  • JESii
    JESii over 4 years
    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.
  • Jonathan Eunice
    Jonathan Eunice over 4 years
    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.
  • SFbay007
    SFbay007 about 4 years
    I don't see this option in iTerm2
  • Konstantin
    Konstantin almost 4 years
    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
    Konstantin almost 4 years
    Oh, well, I was editing the Session Name while what I had to be editing was the Tab Title. Now it works.
  • biesior
    biesior almost 4 years
    My upvote mainly for sample "pony" title :D
  • faximan
    faximan over 2 years
    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 )
  • Krishna Vedula
    Krishna Vedula over 2 years
    This is the best solution. Simple straight forward and works on any shell. Love it
  • wviana
    wviana over 2 years
    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.
  • Admin
    Admin about 2 years
    Doesn't seem to be doing anything in modern (2022) iTerm2
  • Admin
    Admin about 2 years
    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).
  • Admin
    Admin about 2 years
    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.
  • Admin
    Admin about 2 years
    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.
  • Admin
    Admin about 2 years
    This keyboard shortcut has stopped working for me :(