Change iTerm2 window and tab titles in zsh

51

29

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

Posted 2011-06-03T19:30:36.313

Reputation: 1 277

Answers

49

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

Daniel Beck

Posted 2011-06-03T19:30:36.313

Reputation: 98 421

3You also need to set Profiles > Terminal > Terminal Emulation > Terminal may set tab/window title. – vaughan – 2016-04-15T05:58:35.387

1THIS DOES NOT WORK (for me, at least) IN THE LATEST VERSION OF iTerm (3.3.0) – iconoclast – 2019-05-02T17:59:41.107

And doesn't work for me on latest stable version of iTerm2 (3.2.9) – jalanb – 2019-07-19T15:59:08.457

Check my answer. I had the same issue and fixed it. Thanks. – Nuno Gonçalves – 2019-09-12T15:29:04.643

3when 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 – 2011-06-04T05:45:21.210

15Figured 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 – 2011-06-04T05:49:30.953

46

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 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"

joelparkerhenderson

Posted 2011-06-03T19:30:36.313

Reputation: 643

2It still works as of 02/28/2018. – salep – 2018-02-28T16:36:25.887

39

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.

Orangenhain

Posted 2011-06-03T19:30:36.313

Reputation: 653

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. – Mei – 2014-07-23T15:16:57.893

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 – 2015-11-16T14:42:58.180

2I just noticed that the Terminal.app version in OS X Lion supports this as well. – Orangenhain – 2011-10-08T16:50:23.537

silly question: you add these code to .bashrc, right? – qazwsx – 2012-05-02T19:39:39.100

5

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.

Chris Gaudreau

Posted 2011-06-03T19:30:36.313

Reputation: 151

2

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.

Nuno Gonçalves

Posted 2011-06-03T19:30:36.313

Reputation: 121

This isn't under the General tab, it's under the Profiles tab – Falmarri – 2019-09-13T20:32:36.513

Right. I missed the > Profiles one. My bad. Updated. – Nuno Gonçalves – 2019-09-13T22:25:03.933

there isn't Name (Job) as an option now. – dcsan – 2020-02-24T21:02:55.763

1

The accepted answer has worked for me for a long time but is now broken in the latest version of iTerm2. A workaround I found was to enable the Python API and create a script which sets the tab name like so:

#!/usr/bin/env python3.7
import argparse

import iterm2

def get_args():
    parser = argparse.ArgumentParser(description='Set the tab name')
    parser.add_argument('name')
    return parser.parse_args()

ARGS = get_args()

async def main(connection):
    app = await iterm2.async_get_app(connection)
    window = app.current_terminal_window
    if window is not None:
        tab = window.current_tab
        await tab.async_set_title(ARGS.name)
    else:
        # You can view this message in the script console.
        print("No current window")

iterm2.run_until_complete(main)

Saved as "tab_name.py", then invoked with:

~/Library/ApplicationSupport/iTerm2/iterm2env/versions/*/bin/python3 ~/Library/ApplicationSupport/iTerm2/Scripts/tab_name.py "new tab name"

It's not nearly as nice or elegant as the accepted answer, but it works.

Ian E

Posted 2011-06-03T19:30:36.313

Reputation: 11

1

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

https://apple.stackexchange.com/a/90737/49605

michalzuber

Posted 2011-06-03T19:30:36.313

Reputation: 157

2The question specifically says zsh, not bash. – blockloop – 2015-04-30T14:09:24.763

Good, it works in bash! – l mingzhi – 2019-06-05T05:11:58.533

-2

iTerm -> Preferences -> Appearance -> Window & Tab titles -> check Show profile name option

john sam

Posted 2011-06-03T19:30:36.313

Reputation: 101

1

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.

– cascer1 – 2016-11-04T12:05:59.273