terminalapp.plugin.zsh 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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
  4. # Tell the terminal about the working directory whenever it changes.
  5. if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]] && [[ -z "$INSIDE_EMACS" ]]; then
  6. update_terminal_cwd() {
  7. # Identify the directory using a "file:" scheme URL, including
  8. # the host name to disambiguate local vs. remote paths.
  9. # Percent-encode the pathname.
  10. local URL_PATH=''
  11. {
  12. # Use LANG=C to process text byte-by-byte.
  13. local i ch hexch LANG=C
  14. for ((i = 1; i <= ${#PWD}; ++i)); do
  15. ch="$PWD[i]"
  16. if [[ "$ch" =~ [/._~A-Za-z0-9-] ]]; then
  17. URL_PATH+="$ch"
  18. else
  19. hexch=$(printf "%02X" "'$ch")
  20. URL_PATH+="%$hexch"
  21. fi
  22. done
  23. }
  24. local PWD_URL="file://$HOST$URL_PATH"
  25. #echo "$PWD_URL" # testing
  26. printf '\e]7;%s\a' "$PWD_URL"
  27. }
  28. # Register the function so it is called whenever the working
  29. # directory changes.
  30. autoload add-zsh-hook
  31. add-zsh-hook chpwd update_terminal_cwd
  32. # Tell the terminal about the initial directory.
  33. update_terminal_cwd
  34. fi