1 # Set Apple Terminal.app resume directory
2 # based on this answer: http://superuser.com/a/315029
3 # 2012-10-26: (javageek) Changed code using the updated answer
5 # Tell the terminal about the working directory whenever it changes.
6 if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]] && [[ -z "$INSIDE_EMACS" ]]; then
7 update_terminal_cwd
() {
8 # Identify the directory using a "file:" scheme URL, including
9 # the host name to disambiguate local vs. remote paths.
11 # Percent-encode the pathname.
14 # Use LANG=C to process text byte-by-byte.
15 local i ch hexch LANG
=C
16 for ((i
= 1; i
<= ${#PWD}; ++i
)); do
18 if [[ "$ch" =~
[/._~A
-Za-z0-9-] ]]; then
21 hexch
=$(printf "%02X" "'$ch")
27 local PWD_URL
="file://$HOST$URL_PATH"
28 #echo "$PWD_URL" # testing
29 printf '\e]7;%s\a' "$PWD_URL"
32 # Register the function so it is called whenever the working
35 add
-zsh-hook chpwd update_terminal_cwd
37 # Tell the terminal about the initial directory.