40

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.

whiny_nil
  • 403
  • 1
  • 4
  • 4

6 Answers6

31

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

demure
  • 1,331
  • 10
  • 13
  • 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 http://www.faqs.org/docs/Linux-mini/Xterm-Title.html#s3 and http://www.linfo.org/echo.html. Thanks again! – whiny_nil May 12 '13 at 20:30
  • 7
    and to get just the last segment of the path `echo -ne "\033]0;${PWD##*/}\007"` (with thanks to https://github.com/barryclark/bashstrap for showing the way) – Anentropic Dec 23 '13 at 00: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 07 '15 at 09:41
  • Well, exporting is how you use PROMPT_COMMAND... And it is generally better for complicated things [link](https://stackoverflow.com/questions/3058325/what-is-the-difference-between-ps1-and-prompt-command) – demure Dec 08 '15 at 00:34
  • In PowerShell: `Write-Host "\`e]0;$PWD\`a" -NoNewLine` – felixfbecker Sep 10 '18 at 11:32
  • It works but once you ssh into a remote shell the working directory doesn't get updated. Still looking for a solution for that. – kapoko Aug 10 '19 at 10:06
  • Is there a way that I can have this be the default behavior every time I open a new iTerm window? I don't want to have to use the above command every time. – Chris Nielsen Jan 29 '20 at 16:22
  • @ChrisNielsen you would edit your .profile, or your shell's conf file... – demure Jan 30 '20 at 02:50
16

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.

thakis
  • 289
  • 2
  • 5
8

Possibly a simpler answer is to use the string interpolation functionality offered by iTerm 2. Providing:

\(currentTab.currentSession.path)

in settings would result in a changed Window title. The option is accessible through Preferences>>Profiles>>[Profile we need to modify]>>Window

settings Settings

RESULTS Window title

Konrad
  • 747
  • 1
  • 9
  • 20
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
AshCooman
  • 327
  • 2
  • 16
1

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.

rjb
  • 1,241
  • 11
  • 8
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.