For iTerm2, how to make the working directory appear in the window title

itermterminal

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.

Best Answer

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