I recently switched to iTerm2 from the default Terminal app and have a usability issue.

On Terminal, the working directory you are in shows up on the title bar along with the process and pixel size. For example, when you click on Window to switch between different Terminal sessions, you'd see: working_directory - process - pixel_size.

On iTerm2, unfortunately only the process shows in the title bar. So if I'm working in multiple iTerm2 windows on different directories, I can't easily differentiate which is which when I click on Window to choose one. I know I can easily switch between iTerm2 windows once I select one, but I'd like to identify exactly which window I want right away. (By the way, I use Witch, and Command-tabbing doesn't help either because here again only the process shows up on the list).

Any suggestions? I looked through all the preferences and didn't see anything. Creating profiles isn't exactly what I'm after--I'd just like to see the current working directory in iTerm2 window titles.

up vote 17 down vote accepted

With the \033]0;TEXT\007 escape sequence.

Example of use in Bash: echo -ne "\033]0;$PWD\007"

Which you could add to your $PROMPT_COMMAND if you use Bash, or otherwise attach to you PS1 so it gets re-evaluated often.

Example: export PROMPT_COMMAND='echo -ne "\033]0;$PWD\007"'

  • 1
    This works. Just to be pedantic, add the export line listed above to your ~/.bashrc file. – Spencer May 10 '13 at 14:50
  • This is awesome. Thanks so much; this helped me understand that the answer is really about bash programming and has nothing to do with iTerm vs Terminal. As a follow up for anyone else, please see faqs.org/docs/Linux-mini/Xterm-Title.html#s3 and linfo.org/echo.html. Thanks again! – whiny_nil May 12 '13 at 20:30
  • 5
    and to get just the last segment of the path echo -ne "\033]0;${PWD##*/}\007" (with thanks to github.com/barryclark/bashstrap for showing the way) – Anentropic Dec 23 '13 at 0:05
  • @Spencer: There is no reason to export the PROMPT_COMMAND shell variable, and in general one should not export a shell variable unless you have a specific need to share the variable with every program run by the shell. By default, variables that are used by the shell should not be exported; only export variables that are meant to be transmitted to other processes. ~/.bashrc will be run by every interactive shell, so they don't need to inherit the variables set by this script. – Chris Page Dec 7 '15 at 9:41
  • Well, exporting is how you use PROMPT_COMMAND... And it is generally better for complicated things link – demure Dec 8 '15 at 0:34

I like this answer from this gist to add it to the tab title

# put this in your .bash_profile
if [ $ITERM_SESSION_ID ]; then
  export PROMPT_COMMAND='echo -ne "\033];${PWD##*/}\007"; ':"$PROMPT_COMMAND";
fi

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.