termsupport.zsh 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. case "$TERM" in
  17. cygwin|xterm*|putty*|rxvt*|ansi)
  18. print -Pn "\e]2;$2:q\a" # set window name
  19. print -Pn "\e]1;$1:q\a" # set tab name
  20. ;;
  21. screen*)
  22. print -Pn "\ek$1:q\e\\" # set screen hardstatus
  23. ;;
  24. *)
  25. if [[ "$TERM_PROGRAM" == "iTerm.app" ]]; then
  26. print -Pn "\e]2;$2:q\a" # set window name
  27. print -Pn "\e]1;$1:q\a" # set tab name
  28. fi
  29. ;;
  30. esac
  31. }
  32. ZSH_THEME_TERM_TAB_TITLE_IDLE="%15<..<%~%<<" #15 char left truncated PWD
  33. ZSH_THEME_TERM_TITLE_IDLE="%n@%m: %~"
  34. # Avoid duplication of directory in terminals with independent dir display
  35. if [[ $TERM_PROGRAM == Apple_Terminal ]]; then
  36. ZSH_THEME_TERM_TITLE_IDLE="%n@%m"
  37. fi
  38. # Runs before showing the prompt
  39. function omz_termsupport_precmd {
  40. emulate -L zsh
  41. if [[ $DISABLE_AUTO_TITLE == true ]]; then
  42. return
  43. fi
  44. title $ZSH_THEME_TERM_TAB_TITLE_IDLE $ZSH_THEME_TERM_TITLE_IDLE
  45. }
  46. # Runs before executing the command
  47. function omz_termsupport_preexec {
  48. emulate -L zsh
  49. if [[ $DISABLE_AUTO_TITLE == true ]]; then
  50. return
  51. fi
  52. setopt extended_glob
  53. # cmd name only, or if this is sudo or ssh, the next cmd
  54. local CMD=${1[(wr)^(*=*|sudo|ssh|mosh|rake|-*)]:gs/%/%%}
  55. local LINE="${2:gs/%/%%}"
  56. title '$CMD' '%100>...>$LINE%<<'
  57. }
  58. precmd_functions+=(omz_termsupport_precmd)
  59. preexec_functions+=(omz_termsupport_preexec)
  60. # Keep Apple Terminal.app's current working directory updated
  61. # Based on this answer: http://superuser.com/a/315029
  62. # With extra fixes to handle multibyte chars and non-UTF-8 locales
  63. if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]] && [[ -z "$INSIDE_EMACS" ]]; then
  64. # Emits the control sequence to notify Terminal.app of the cwd
  65. function update_terminalapp_cwd() {
  66. emulate -L zsh
  67. # Identify the directory using a "file:" scheme URL, including
  68. # the host name to disambiguate local vs. remote paths.
  69. # Percent-encode the pathname.
  70. local URL_PATH="$(omz_urlencode -P $PWD)"
  71. [[ $? != 0 ]] && return 1
  72. local PWD_URL="file://$HOST$URL_PATH"
  73. # Undocumented Terminal.app-specific control sequence
  74. printf '\e]7;%s\a' $PWD_URL
  75. }
  76. # Use a precmd hook instead of a chpwd hook to avoid contaminating output
  77. precmd_functions+=(update_terminalapp_cwd)
  78. # Run once to get initial cwd set
  79. update_terminalapp_cwd
  80. fi