This subject has grown into a book: reworked and expanded with more detail and topics. Like my other books, I plan to update and add to it after release as well, keeping it relevant and useful. You can order it on the Apple Books Store now.
While I was working on customizing my zsh
configuration files for my zsh
article series, I noticed that Terminal would not display the current working directory when using zsh
. it worked fine when I switched back to bash
.
Note: showing the working directory in the window or tab title is enabled by default in Terminal. You can configure what Terminal shows in the title in the Preferences Window for each profile. Select a profile and
Some sleuthing showed me that /etc/bashrc_Apple_Terminal
sets up a update_terminal_cwd
function which sends some escape codes to Terminal to keep the window title bar updated. This file is sourced by /etc/bashrc
for the Terminal.app.
In macOS 10.14 Mojave and earlier, this configuration file has no equivalent for zsh
. /etc/zshrc
has code that would load /etc/zshrc_Apple_Terminal
, if it existed. However, this file does not exist in macOS Mojave and earlier.
It does in Catalina, though, and it sets up a similar function that is added into the precmd
hook. If you have access to the Catalina beta, you can just copy the /etc/zshrc_Apple_Terminal
to your Mojave (or earlier) Mac and it will work.
Alternatively, you can write your own implementation, which is what I did, because I wanted it before the Catalina files existed. My solution exists of the function and its setup in its own file. This file is in a directory that is added to the fpath
so I can simply load it with autoload
.
You can see the code for the function file here.
Then, in my .zshrc
I load the function file with:
# path to my zsh functions folder:
my_zsh_functions=$repo_dir/dotfiles/zshfunctions/
# include my zshfunctions dir in fpath:
if [[ -d $my_zsh_functions ]]; then
fpath=( $my_zsh_functions $fpath )
fi
# only for Mojave and earlier
if [[ $(sw_vers -buildVersion) < "19" ]]; then
# this sets up the connection with the Apple Terminal Title Bar
autoload -U update_terminal_pwd && update_terminal_pwd
fi
And from now, the title will be updated in Mojave (and earlier) and the function won’t even be loaded in Catalina.
This might seem a bit overkill, now that the functionality is in the Catalina file, but it might serve as a useful example when you want to customize, or re-implement similar behavior.