Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
ZSH + Apple Terminal: set tab title current working directory for Mojave and earlier
# This is a slightly modified version of Mojave (10.14) script located in /etc/bashrc_Apple_Terminal
# modified to work with zsh.
# It mainly calls update_terminal_cwd when changing the working directory, which prints a special
# invisible string that Apple Terminal interprets and updates the current term title accordingly.
# Note : this script is useless for macOS Catalina and newer versions of macOS, since it should
# already contain at least this functionality.
# Instructions
# Place this file in /etc/ (alongside bash_Apple_Terminal)
update_terminal_cwd() {
# Identify the directory using a "file:" scheme URL, including
# the host name to disambiguate local vs. remote paths.
# Percent-encode the pathname.
local url_path=''
# Use LC_CTYPE=C to process text byte-by-byte. Ensure that
# LC_ALL isn't set, so it doesn't interfere.
local i
local ch
local hexch
local LC_CTYPE=C
local LC_ALL=
for ((i = 0; i < ${#PWD}; ++i)); do
if [[ "$ch" =~ [/._~A-Za-z0-9-] ]]; then
printf -v hexch "%02X" "'$ch"
# printf treats values greater than 127 as
# negative and pads with "FF", so truncate.
url_path+="%${hexch: -2:2}"
printf '\e]7;%s\a' "file://$HOSTNAME$url_path"
if [ -z "$INSIDE_EMACS" ]; then
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment