Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Set the iTerm tab title to the current directory, not full path.
# put this in your .bash_profile
if [ $ITERM_SESSION_ID ]; then
export PROMPT_COMMAND='echo -ne "\033];${PWD##*/}\007"; ':"$PROMPT_COMMAND";
fi
# Piece-by-Piece Explanation:
# the if condition makes sure we only screw with $PROMPT_COMMAND if we're in an iTerm environment
# iTerm happens to give each session a unique $ITERM_SESSION_ID we can use, $ITERM_PROFILE is an option too
# the $PROMPT_COMMAND environment variable is executed every time a command is run
# see: ss64.com/bash/syntax-prompt.html
# we want to update the iTerm tab title to reflect the current directory (not full path, which is too long)
# echo -ne "\033;foo\007" sets the current tab title to "foo"
# see: stackoverflow.com/questions/8823103/how-does-this-script-for-naming-iterm-tabs-work
# the two flags, -n = no trailing newline & -e = interpret backslashed characters, e.g. \033 is ESC, \007 is BEL
# see: ss64.com/bash/echo.html for echo documentation
# we set the title to ${PWD##*/} which is just the current dir, not full path
# see: stackoverflow.com/questions/1371261/get-current-directory-name-without-full-path-in-bash-script
# then we append the rest of $PROMPT_COMMAND so as not to remove what was already there
# voilà!
@phette23

This comment has been minimized.

Copy link
Owner Author

commented Mar 29, 2013

It took me way too long to figure this out so I've put in all the documentation I could find.

@tonatiuh

This comment has been minimized.

Copy link

commented Mar 12, 2014

Thanks a lot.

@tjmcewan

This comment has been minimized.

Copy link

commented Jul 18, 2014

This gist is bash specific, but my googling landed me here, so in case you're using zsh, try this:

precmd() {
  # sets the tab title to current dir
  echo -ne "\e]1;${PWD##*/}\a"
}

I pieced it together from this gist & this superuser answer. Note: I removed the iTerm-specific guard because Terminal.app also supports this now.

@phaseOne

This comment has been minimized.

Copy link

commented Jul 25, 2014

When in tmux in iTerm2, I get this message every command:

-bash: :echo: command not found

Any ideas?

@caseycs

This comment has been minimized.

Copy link

commented Sep 4, 2014

Thanks! Good job!

@nirnaeth

This comment has been minimized.

Copy link

commented Oct 8, 2014

thank you veeeeery much!

@jmsmrgn

This comment has been minimized.

Copy link

commented Nov 12, 2014

@phaseOne - this clears up iterm command not found

'export PROMPT_COMMAND='echo -ne "\033]0;${PWD##*/}\007"''

https://github.com/barryclark/bashstrap/blob/master/.bash_profile#L105

@ericdorsey

This comment has been minimized.

Copy link

commented Mar 19, 2015

This is a fantastic addition to my .bash_profile. Thanks!

@countcain

This comment has been minimized.

Copy link

commented Jul 3, 2015

This is awesome.

@chris-schmitz

This comment has been minimized.

Copy link

commented Aug 25, 2015

Just what I was looking for. Thanks!

@vgaviria

This comment has been minimized.

Copy link

commented Nov 19, 2015

Appreciate the Gist. This is exactly what I was looking for!

@cybertk

This comment has been minimized.

Copy link

commented Dec 1, 2015

Awesome! +1

@clarkenheim

This comment has been minimized.

Copy link

commented May 2, 2016

You are a gentleman sir.

@spindelmanne

This comment has been minimized.

Copy link

commented May 10, 2016

Great stuff! Thanks!

@mhdkassir

This comment has been minimized.

Copy link

commented Jun 8, 2016

Thanks! very useful 👍

@alexey-danilov

This comment has been minimized.

Copy link

commented Dec 3, 2016

This does not seem to be working in iTerm version 3.0.12

@bradj

This comment has been minimized.

Copy link

commented Dec 6, 2016

@alexey-danilov confirmed for me as well.

@markbuckalewcb

This comment has been minimized.

Copy link

commented Dec 8, 2016

^^

@alexilyaev

This comment has been minimized.

Copy link

commented Dec 31, 2016

If you get this:

-bash: :echo: command not found

Or you want to be able to add several commands to PROMPT_COMMAND, this is what I use:

# Add command to PROMPT_COMMAND (runs before each command)
# Makes sure ithe command is not already in PROMPT_COMMAND
addToPromptCommand() {
  if [[ ":$PROMPT_COMMAND:" != *":$1:"* ]]; then
    PROMPT_COMMAND="${PROMPT_COMMAND:+"$PROMPT_COMMAND:"}$1"
  fi
}

# Set iTerm title to show current directory
if [ $ITERM_SESSION_ID ]; then
  addToPromptCommand 'echo -ne "\033];${PWD##*/}\007"'
fi

Based on:
http://superuser.com/questions/39751/add-directory-to-path-if-its-not-already-there

@twei55

This comment has been minimized.

Copy link

commented Jan 5, 2017

Great and thanks! 🥇

@buraksay

This comment has been minimized.

Copy link

commented Jan 22, 2017

What worked for me was this (on MacOS sierra):

export PROMPT_COMMAND="$PROMPT_COMMAND : echo -n '\033]0;${PWD##*/}\007' "
@kud

This comment has been minimized.

Copy link

commented Feb 1, 2017

@jeff-kilbride

This comment has been minimized.

Copy link

commented Feb 13, 2017

The following two commands in .bash_profile will change the tab title when you cd into a new directory (as well as list the directory contents without having to type ls ...):

# Set iTerm2 tab titles
tabTitle() { echo -ne "\033]0;"$*"\007"; }

# Always list directory contents and set title upon 'cd'
cd() { builtin cd "$@"; ls -lFah; tabTitle ${PWD##*/}; }

This also lets you set the title with tabTitle foobar from any prompt. Works great on sierra!

@Pierre-Thibault

This comment has been minimized.

Copy link

commented Mar 8, 2017

Thank you!

@kurko

This comment has been minimized.

Copy link

commented Mar 12, 2017

Only @alexilyaev worked for me on tmux.

@seantcanavan

This comment has been minimized.

Copy link

commented Mar 15, 2017

None of these worked for me in iterm2 Build 3.0.14 on macOS Sierra 10.12.3 (16D32).

They either overwrote what I put in my PS_1 entry or did nothing whatsoever. My tabs and indeed my iterm2 window were always just named 'bash'. I'm trying to get the full working directory in the tab name.

@jameswise-ft

This comment has been minimized.

Copy link

commented Sep 11, 2017

You're a lifesaver, this makes my life so much easier.
Thank you!

@jeromecovington

This comment has been minimized.

Copy link

commented Sep 22, 2017

@phette23, will the gist that you provided (thanks for taking the time to do so), show the current working directory and the current process?

@ozydingo

This comment has been minimized.

Copy link

commented Oct 11, 2017

@jeromecovington if you have the iterm settings checked to show the current job name, this will be appended.

If you mean process id (not sure if that's really useful but hey why not try), you can use the bash variable $$, and anything else you can think of doing in bash (e.g. current git branch, current user, etc). For the process id, I proof-of-concepted this with this modification:

export PROMPT_COMMAND='echo -ne "\033];($$) ${PWD##*/}\007"; ':"$PROMPT_COMMAND";

@ozydingo

This comment has been minimized.

Copy link

commented Oct 11, 2017

@phette23 thanks for this, it's really helpful!

I've added two lines of comment-docs to ensure that people have the correct setting in iTerm to allow this change to take effect -- without the "terminal may set tab/window title" setting checked this will not work. This may be why some people are having trouble.

See https://gist.github.com/ozydingo/cc4d400fcf754ae0f1456ab7240be604 . Could you add these two comments, in some form, to this gist?

@danio

This comment has been minimized.

Copy link

commented Mar 8, 2018

Using the agnoster theme that also sets the prompt to show the current git branch, I found that it no longer worked correctly, even using @alexilyaev's suggestion. The only way I could get it to work for me is:

# after calling source $THEME
setTabTitle() {
  echo -ne "\033]0;${PWD##*/}\007"
}
if [ $ITERM_SESSION_ID ]; then
  export PROMPT_COMMAND="setTabTitle;$PROMPT_COMMAND"
fi

(thanks to https://stackoverflow.com/a/36669770/12663)

@CavalcanteLeo

This comment has been minimized.

Copy link

commented Apr 5, 2018

if [ $ITERM_SESSION_ID ]; then
  DISABLE_AUTO_TITLE="true"
  echo -ne "\033];${PWD##*/}\007"
fi

precmd() {
  echo -ne "\033];${PWD##*/}\007"
}

also use precmd function, to watch directories changes.
Alto disable auto title as well. DISABLE_AUTO_TITLE="true"

@wsshin

This comment has been minimized.

Copy link

commented Jul 18, 2018

To show not just the current directory, but also the immediate parent directory, use this:

if [ $ITERM_SESSION_ID ]; then
    export PROMPT_COMMAND='i="$IFS";IFS='/';set -f;p=($PWD);set +f;IFS="$i";if [ ${#p[@]} -le 3 ];then echo -ne "\033];$PWD\007";else echo -ne "\033];${p[-2]}/${p[-1]}\007";fi;':$PROMPT_COMMAND
fi

Showing the immediate parent directory is useful when you work on several different projects with similar directory structures. For example, if you work in ~/Documents/Project1/src in one tab and ~/Documents/Project2/src in another tab, the above trick shows Project1/src and Project2/src as tab titles so that they can be distinguished easily.

For tokenizing $PWD, the credit goes to https://stackoverflow.com/a/35782319/5932368.

EDIT: the command is updated to handle p with less than three tokens.

@scumbkt19

This comment has been minimized.

Copy link

commented Oct 2, 2018

I ran into this looking for a way to mark the current git repo i'm in. This version will display the git repository root (normally the repo name) if you're in a repository, or it will fall back to show the current working direcory

if [ $ITERM_SESSION_ID ]; then
  export PROMPT_COMMAND='git rev-parse --is-inside-work-tree > /dev/null 2>&1 && echo -ne "\033];$(basename $(git rev-parse --show-toplevel))\007" || echo -ne "\033];${PWD##*/}\007"; ':"$PROMPT_COMMAND";
fi
@bertolin

This comment has been minimized.

Copy link

commented Nov 16, 2018

Many thanks for this helpful stuff!

@jhrr

This comment has been minimized.

Copy link

commented Nov 21, 2018

Here's a more legible version of @scumbkt19's snippet:

if [[ $ITERM_SESSION_ID ]]; then
  # Display the current git repo, or directory, in iterm tabs.
  get_iterm_label() {
    if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
      local directory
      directory=${PWD##*/}
      echo -ne "\\033];$directory\\007"
    else
      local branch
      branch=$(basename "$(git rev-parse --show-toplevel)")
      echo -ne "\\033];$branch\\007"
    fi
  }
  export PROMPT_COMMAND=get_iterm_label;"${PROMPT_COMMAND}"
fi
@jwmann

This comment has been minimized.

Copy link

commented Jan 12, 2019

To expand on @jhrr I've added the current branch to my tab title.

    if [[ $ITERM_SESSION_ID ]]; then
      # Display the current git repo, or directory, in iterm tabs.
      get_iterm_label() {
        if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
          local directory
          directory=${PWD##*/}
          echo -ne "\\033];$directory\\007"
        else
          local branch
          local branchdir
          branchdir=$(basename "$(git rev-parse --show-toplevel)")
          branch=$(git branch 2>/dev/null | grep -e '\* ' | sed "s/^..\(.*\)/{\1}/")
          echo -ne "\\033];$branchdir $branch\\007"
        fi
      }
      export PROMPT_COMMAND=get_iterm_label;"${PROMPT_COMMAND}"
    fi
@tsiq-pete

This comment has been minimized.

Copy link

commented Jun 4, 2019

Thank you @jwmann.

@dhruv-m-patel

This comment has been minimized.

Copy link

commented Aug 21, 2019

Simple and elegant. This is a gem. 👍 (iTerm v3.3.2)

@garyking

This comment has been minimized.

Copy link

commented Sep 5, 2019

Did this suddenly stop working in iTerm2 v3.3.3 for anyone else than me? Does anyone know how to fix that?

@runtimee

This comment has been minimized.

Copy link

commented Sep 5, 2019

Stopped working for me this morning :(

@phette23

This comment has been minimized.

Copy link
Owner Author

commented Sep 5, 2019

@garyking @runtimee did you try Profiles > (your profile) > Terminal > [x] "Terminal may report window title"? I'm not 100% certain that's the cause but I had the same issue, selected that, opened a new tab in the profile and it worked again.

FWIW I use Fish shell now which just has a fish_title function you can override so I don't really check in on this snippet.

@andreasmihm

This comment has been minimized.

Copy link

commented Sep 6, 2019

stopped working for me too

@fmmatheus

This comment has been minimized.

Copy link

commented Sep 6, 2019

stopped working for me too [2]. Checking the Profiles > (your profile) > Terminal > [x] "Terminal may report window title" didn't work for me.

@jjpmann

This comment has been minimized.

Copy link

commented Sep 6, 2019

Mine also stopped working.
iTerm2 v3.3.3
OSX 10.14.6
Rolled back to iTerm2 v3.3.2 did not fix it.

@dvbowen

This comment has been minimized.

Copy link

commented Sep 7, 2019

The tcsh version I had to do this (which I copied from here long, long ago) also stopped working this evening after I logged in, for no obvious reason.

iTerm2 v3.3.3
macOS 10.14.6

Kind of a disaster for me.....

@nerdondon

This comment has been minimized.

Copy link

commented Sep 8, 2019

according to the changelog for 3.3.3 on https://iterm2.com/downloads.html says

Improvements
- Change the default tab/window title to show the
  job name.

it might have made changes to the default profile if that was the one you were using "/

@dvbowen

This comment has been minimized.

Copy link

commented Sep 8, 2019

Ah! That was really useful, many thanks. The solution appears to be fiddle with Preferences->General and change the "Title" box to something (like "Name" -- I also unchecked "Job Name") which allows the "Applications in terminal may change the title" box to be checked. After I did that, the directory names came back as the tab titles, so I guess the script I run is allowed through.

@seanhamlin

This comment has been minimized.

Copy link

commented Sep 8, 2019

Thanks @dvbowen, that worked perfectly.

@dcastro

This comment has been minimized.

Copy link

commented Sep 9, 2019

Thanks @dvbowen!
For clarity, here's what Preferences > Profile should look like:

Screenshot 2019-09-09 at 14 06 38
Screenshot 2019-09-09 at 14 06 48

And .zshrc:

DISABLE_AUTO_TITLE="true"

precmd() {
  # sets the tab title to current dir
  echo -ne "\e]1;${PWD##*/}\a"
}
@fmmatheus

This comment has been minimized.

Copy link

commented Sep 9, 2019

The above solution worked for me. 👍

@nunogoncalves

This comment has been minimized.

Copy link

commented Sep 12, 2019

Thanks for that too @dcastro.
I added an answer here: https://superuser.com/a/1481956/767288 with your suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.