termsupport.zsh 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. if [[ $2 == "" ]]; then
  11. 2="$1"
  12. fi
  13. if [[ "$EMACS" == *term* ]]; then
  14. return
  15. fi
  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. # Runs before showing the prompt
  26. function omz_termsupport_precmd {
  27. if [[ $DISABLE_AUTO_TITLE == true ]]; then
  28. return
  29. fi
  30. title $ZSH_THEME_TERM_TAB_TITLE_IDLE $ZSH_THEME_TERM_TITLE_IDLE
  31. # Notify Terminal.app of current directory using undocumented OSC sequence
  32. # found in OS X 10.9 and 10.10's /etc/bashrc
  33. if [[ $TERM_PROGRAM == Apple_Terminal ]] && [[ -z $INSIDE_EMACS ]]; then
  34. local PWD_URL="file://$HOSTNAME${PWD// /%20}"
  35. printf '\e]7;%s\a' "$PWD_URL"
  36. fi
  37. }
  38. # Runs before executing the command
  39. function omz_termsupport_preexec {
  40. if [[ $DISABLE_AUTO_TITLE == true ]]; then
  41. return
  42. fi
  43. emulate -L zsh
  44. setopt extended_glob
  45. # cmd name only, or if this is sudo or ssh, the next cmd
  46. local CMD=${1[(wr)^(*=*|sudo|ssh|rake|-*)]:gs/%/%%}
  47. local LINE="${2:gs/%/%%}"
  48. title '$CMD' '%100>...>$LINE%<<'
  49. }
  50. precmd_functions+=(omz_termsupport_precmd)
  51. preexec_functions+=(omz_termsupport_preexec)