Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 34 down vote accepted

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

share|improve this answer
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 Jun 4 '11 at 5:45
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 – bneil Jun 4 '11 at 5:49
You also need to set Profiles > Terminal > Terminal Emulation > Terminal may set tab/window title. – vaughan Apr 15 at 5:58

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.

share|improve this answer
I just noticed that the version in OS X Lion supports this as well. – Orangenhain Oct 8 '11 at 16:50
silly question: you add these code to .bashrc, right? – qazwsx May 2 '12 at 19:39
@duperuser That's right. – Orangenhain May 3 '12 at 8:21
Also appears that (in iTerm2 Build 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. – Mei Jul 23 '14 at 15:16
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 – user15681 Nov 16 '15 at 14:42

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 zsh, then edit your settings, typically in ~/.zshrc, so you have this line:

share|improve this answer
This should be the selected answer – nonocut Feb 24 at 10:58

Adding export PROMPT_COMMAND='echo -ne "\033]0;$PWD\007"' into ~/.bash_profile worked for me.

share|improve this answer
The question specifically says zsh, not bash. – blockloop Apr 30 '15 at 14:09

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
  echo -ne "$window_title"

add-zsh-hook precmd set-window-title

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

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .