Change iTerm2 window and tab titles in zsh

53,175

Solution 1

You can enter the following in zsh to set the window title of iTerm2:

echo -ne "\e]1;this is the title\a"

If you want to automate that to insert e.g. the current time or working directory, edit your zsh configuration files to set the title in the precmd() function to e.g. $PWD.

echo -ne "\e]1;$PWD\a"

You can read about the precmd function in man zshmisc in the section SPECIAL FUNCTIONS.

enter image description here

Solution 2

What works for me:

echo -e "\033];this is the title\007"

If you use Mac OSX and iTerm, iTerm2::

  • iTerm → Preferences → Appearance → Window & Tab Titles → uncheck all

If you use Oh My Zsh, then you may need to edit your settings. Your settings are typically in the file ~/.zshrc. You want to add or edit your settings to make sure this line exists:

DISABLE_AUTO_TITLE="true"

Solution 3

One of the amenities of using iTerm is the possibility of setting window title & tab title separately: example of using tab & window title separately

# $1 = type; 0 - both, 1 - tab, 2 - title
# rest = text
setTerminalText () {
    # echo works in bash & zsh
    local mode=$1 ; shift
    echo -ne "\033]$mode;$@\007"
}
stt_both  () { setTerminalText 0 $@; }
stt_tab   () { setTerminalText 1 $@; }
stt_title () { setTerminalText 2 $@; }

This way you can immediately see what host you're connected to in what window, and the window title for each tab shows user & CWD.

Solution 4

A precmd does the trick. However, some oh-my-zsh themes mess around with the window title. Set PR_TITLEBAR to an empty string to fix it.

set-window-title() {
  # /Users/clessg/projects/dotfiles -> ~/p/dotfiles
  window_title="\e]0;${${PWD/#"$HOME"/~}/projects/p}\a"
  echo -ne "$window_title"
}

PR_TITLEBAR=''
set-window-title
add-zsh-hook precmd set-window-title

I would also recommend playing around with the tab settings of iTerm2 in Preferences -> Appearance.

Solution 5

None of the answers seemed to work for me, probably for the iterm2 version (3.3.3).

I found this out: https://gist.github.com/phette23/5270658#gistcomment-3020766

Essentially, you can do whatever is said in all other answers, but also need to set Preferences > Profiles > General > Title -> Name (Job)

This worked for me.

Share:
53,175

Related videos on Youtube

bneil
Author by

bneil

Updated on September 18, 2022

Comments

  • bneil
    bneil over 1 year

    I want to label the window tabs of terminal sessions. I'm using the zshell in iterm2 on OSX. Is it possible to change the label of a window tab dynamically in the terminal?

  • bneil
    bneil almost 13 years
    when I execute the command echo -ne "\e]1;this is the title\a" the tab title does not change. Is there a setting I have to change in iterm2?
  • bneil
    bneil almost 13 years
    Figured it out. I have to deselect all the options for window and tab titles in the iterm->preferences->appearance section. and in the .zshrc I have to uncomment/add export DISABLE_AUTO_TITLE="true" Thanks @Daniel Beck
  • Orangenhain
    Orangenhain over 12 years
    I just noticed that the Terminal.app version in OS X Lion supports this as well.
  • qazwsx
    qazwsx almost 12 years
    silly question: you add these code to .bashrc, right?
  • Mei
    Mei almost 10 years
    Also appears that (in iTerm2 Build 1.0.0.20140629) until you change the Window Title... it tracks the tab title, no matter what. I'm sure that's a setting. Once the Window title is changed (2) it no longer tracks tab title.
  • blockloop
    blockloop almost 9 years
    The question specifically says zsh, not bash.
  • user15681
    user15681 over 8 years
    I modified the function above to include the line DISABLE_AUTO_TITLE="true" that fixed the issue and this way if I don't set the title, I still get the automatic title feature
  • vaughan
    vaughan about 8 years
    You also need to set Profiles > Terminal > Terminal Emulation > Terminal may set tab/window title.
  • Cas
    Cas over 7 years
    Welcome to Super User! On this Q&A site we try to provide good answers to questions people ask. Part of writing a good answer is providing context for the proposed solution. Please edit your answer and explain why your solution works, and what, specifically, it does.
  • salep
    salep about 6 years
    It still works as of 02/28/2018.
  • iconoclast
    iconoclast almost 5 years
    THIS DOES NOT WORK (for me, at least) IN THE LATEST VERSION OF iTerm (3.3.0)
  • Ferris
    Ferris almost 5 years
    Good, it works in bash!
  • jalanb
    jalanb almost 5 years
    And doesn't work for me on latest stable version of iTerm2 (3.2.9)
  • Nuno Gonçalves
    Nuno Gonçalves over 4 years
    Check my answer. I had the same issue and fixed it. Thanks.
  • Falmarri
    Falmarri over 4 years
    This isn't under the General tab, it's under the Profiles tab
  • Nuno Gonçalves
    Nuno Gonçalves over 4 years
    Right. I missed the > Profiles one. My bad. Updated.
  • dcsan
    dcsan about 4 years
    there isn't Name (Job) as an option now.
  • cambunctious
    cambunctious about 4 years
    DISABLE_AUTO_TITLE is an Oh My Zsh feature, not zsh
  • Graham P Heath
    Graham P Heath almost 4 years
    But there is Job Name...
  • azureai
    azureai almost 4 years
    This doesn't work anymore in iTerm2 3.3.10, even when doing what @NunoGonçalves wrote.
  • azureai
    azureai almost 4 years
    This doesn't work.
  • mojave
    mojave over 3 years
    i'm using Oh My Zsh, and the DISABLE_AUTO_TITLE fixed it for me.
  • Franklin Yu
    Franklin Yu over 3 years
    That’s weird; it works for me in iTerm2 3.3.12.
  • HappyFace
    HappyFace over 3 years
    I had to do prompt_pure_set_title() true # disables pure setting the title. Your own theme might have sth similar.