26

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.

24

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

| improve this answer | |
  • 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
  • 1
    @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
2

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
| improve this answer | |
1

If you're using zsh (which is the default in macOS 10.15), put this in your ~/.zshrc instead:

if [ $ITERM_SESSION_ID ]; then
precmd() {
  echo -ne "\033]0;${PWD##*/}\007"
}
fi

You can also spell \033 as \e and \007 as \a.

| improve this answer | |
0

You can relatively easily set the tab title in iTerm2 on macOS to the current working directory by placing the following in your ~/.zshrc profile:

# Set iTerm Tab Title to Current Working Directory
DISABLE_AUTO_TITLE="true"

iterm_tab_title() {
  echo -ne "\e]0;${PWD##*/}\a"
}
add-zsh-hook precmd iterm_tab_title

It uses a precmd hook function to execute before each prompt, setting the tab title by echoing out the current working directory.

For good measure, some Oh My Zsh themes mess around with the Terminal window title, so setting DISABLE_AUTO_TITLE="true" fixes this.

| improve this answer | |
0

Don't have enough karma to comment, but if you want it to show ~/dir instead of /users/username/dir you can use

if [ $ITERM_SESSION_ID ]; then
precmd() {
  echo -ne "\033]0;$(dirs)\007"
}
fi

instead.

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .