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?
9 Answers
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.

- 111,893
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"
- 519
- 763
- 6
- 9
One of the amenities of using iTerm is the possibility of setting window title & tab 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.
- 743
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.
- 161
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.
- 151
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.
- 131
Don't forget to check Profile -> {yourProfile} -> General -> "Application in terminal may change the title"
If you don't do this, your session name will be locked unless you unlock manually.
- 21
Adding export PROMPT_COMMAND='echo -ne "\033]0;$PWD\007"' into ~/.bash_profile worked for me.
- 167
iTerm -> Preferences -> Appearance -> Window & Tab titles -> check Show profile name option
- 101