Last active
April 7, 2021 14:30
-
-
Save habovh/02b825e6511af8a918c2dfbee10b2b9c to your computer and use it in GitHub Desktop.
ZSH + Apple Terminal: set tab title current working directory for Mojave and earlier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
ch="${PWD:$i:1}" | |
if [[ "$ch" =~ [/._~A-Za-z0-9-] ]]; then | |
url_path+="$ch" | |
else | |
printf -v hexch "%02X" "'$ch" | |
# printf treats values greater than 127 as | |
# negative and pads with "FF", so truncate. | |
url_path+="%${hexch: -2:2}" | |
fi | |
done | |
} | |
printf '\e]7;%s\a' "file://$HOSTNAME$url_path" | |
} | |
if [ -z "$INSIDE_EMACS" ]; then | |
chpwd_functions+=('update_terminal_cwd') | |
update_terminal_cwd | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment