displaying current working directory in iterm window

2020-04-29

 | 

~1 min read

 | 

163 words

Understanding where you are in your terminal can be a useful piece of information.

While you can always find it by printing the working directory, certain terminals can provide that information for you in the UI.

For example, in iTerm, it can be the name of the window.

To set it, I used the precmd function in zsh: 1

./zshrc
# use precmd to name the window:
# e.g., /Users/stephen/projects/dotfiles -> ~/p/dotfiles
function precmd() {
  window_title="\e]0;${${PWD/#"$HOME"/~}/projects/p}\a"
  echo -ne "$window_title"
}

Once you save this function and reload your terminal, you can start to see the results.

Before we made the change:

The terminal before we added the precmd function

Once refreshed, the precmd function fired and updated our window immediately: Post change - blog

And it will change anytime the directory changes: Post change - root directory

Footnotes

  • 1 For more options take a look at the rest of the hook functions in section 9.3.1 of the zsh manual

Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!