termsupport.zsh 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Set terminal window and tab/icon title
  2. #
  3. # usage: title short_tab_title [long_window_title]
  4. #
  5. # See: http://www.faqs.org/docs/Linux-mini/Xterm-Title.html#ss3.1
  6. # Fully supports screen, iterm, and probably most modern xterm and rxvt
  7. # (In screen, only short_tab_title is used)
  8. # Limited support for Apple Terminal (Terminal can't set window and tab separately)
  9. function title {
  10. emulate -L zsh
  11. setopt prompt_subst
  12. [[ "$EMACS" == *term* ]] && return
  13. # if $2 is unset use $1 as default
  14. # if it is set and empty, leave it as is
  15. : ${2=$1}
  16. if [[ "$TERM" == screen* ]]; then
  17. print -Pn "\ek$1:q\e\\" #set screen hardstatus, usually truncated at 20 chars
  18. elif [[ "$TERM" == xterm* ]] || [[ "$TERM" == rxvt* ]] || [[ "$TERM" == ansi ]] || [[ "$TERM_PROGRAM" == "iTerm.app" ]]; then
  19. print -Pn "\e]2;$2:q\a" #set window name
  20. print -Pn "\e]1;$1:q\a" #set icon (=tab) name
  21. fi
  22. }
  23. ZSH_THEME_TERM_TAB_TITLE_IDLE="%15<..<%~%<<" #15 char left truncated PWD
  24. ZSH_THEME_TERM_TITLE_IDLE="%n@%m: %~"
  25. # Avoid duplication of directory in terminals with independent dir display
  26. if [[ $TERM_PROGRAM == Apple_Terminal ]]; then
  27. ZSH_THEME_TERM_TITLE_IDLE="%n@%m"
  28. fi
  29. # Runs before showing the prompt
  30. function omz_termsupport_precmd {
  31. emulate -L zsh
  32. if [[ $DISABLE_AUTO_TITLE == true ]]; then
  33. return
  34. fi
  35. title $ZSH_THEME_TERM_TAB_TITLE_IDLE $ZSH_THEME_TERM_TITLE_IDLE
  36. }
  37. # Runs before executing the command
  38. function omz_termsupport_preexec {
  39. emulate -L zsh
  40. if [[ $DISABLE_AUTO_TITLE == true ]]; then
  41. return
  42. fi
  43. setopt extended_glob
  44. # cmd name only, or if this is sudo or ssh, the next cmd
  45. local CMD=${1[(wr)^(*=*|sudo|ssh|mosh|rake|-*)]:gs/%/%%}
  46. local LINE="${2:gs/%/%%}"
  47. title '$CMD' '%100>...>$LINE%<<'
  48. }
  49. precmd_functions+=(omz_termsupport_precmd)
  50. preexec_functions+=(omz_termsupport_preexec)
  51. # Keep Apple Terminal.app's current working directory updated
  52. # Based on this answer: http://superuser.com/a/315029
  53. # With extra fixes to handle multibyte chars and non-UTF-8 locales
  54. if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]] && [[ -z "$INSIDE_EMACS" ]]; then
  55. # Emits the control sequence to notify Terminal.app of the cwd
  56. function update_terminalapp_cwd() {
  57. emulate -L zsh
  58. # Identify the directory using a "file:" scheme URL, including
  59. # the host name to disambiguate local vs. remote paths.
  60. # Percent-encode the pathname.
  61. local URL_PATH=$(omz_urlencode -P $PWD)
  62. [[ $? != 0 ]] && return 1
  63. local PWD_URL="file://$HOST$URL_PATH"
  64. # Undocumented Terminal.app-specific control sequence
  65. printf '\e]7;%s\a' $PWD_URL
  66. }
  67. # Use a precmd hook instead of a chpwd hook to avoid contaminating output
  68. precmd_functions+=(update_terminalapp_cwd)
  69. # Run once to get initial cwd set
  70. update_terminalapp_cwd
  71. fi