termsupport.zsh 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. [[ "$EMACS" == *term* ]] && return
  11. # if $2 is unset use $1 as default
  12. # if it is set and empty, leave it as is
  13. : ${2=$1}
  14. if [[ "$TERM" == screen* ]]; then
  15. print -Pn "\ek$1:q\e\\" #set screen hardstatus, usually truncated at 20 chars
  16. elif [[ "$TERM" == xterm* ]] || [[ "$TERM" == rxvt* ]] || [[ "$TERM" == ansi ]] || [[ "$TERM_PROGRAM" == "iTerm.app" ]]; then
  17. print -Pn "\e]2;$2:q\a" #set window name
  18. print -Pn "\e]1;$1:q\a" #set icon (=tab) name
  19. fi
  20. }
  21. ZSH_THEME_TERM_TAB_TITLE_IDLE="%15<..<%~%<<" #15 char left truncated PWD
  22. ZSH_THEME_TERM_TITLE_IDLE="%n@%m: %~"
  23. # Runs before showing the prompt
  24. function omz_termsupport_precmd {
  25. if [[ $DISABLE_AUTO_TITLE == true ]]; then
  26. return
  27. fi
  28. title $ZSH_THEME_TERM_TAB_TITLE_IDLE $ZSH_THEME_TERM_TITLE_IDLE
  29. }
  30. # Runs before executing the command
  31. function omz_termsupport_preexec {
  32. if [[ $DISABLE_AUTO_TITLE == true ]]; then
  33. return
  34. fi
  35. emulate -L zsh
  36. setopt extended_glob
  37. # cmd name only, or if this is sudo or ssh, the next cmd
  38. local CMD=${1[(wr)^(*=*|sudo|ssh|mosh|rake|-*)]:gs/%/%%}
  39. local LINE="${2:gs/%/%%}"
  40. title '$CMD' '%100>...>$LINE%<<'
  41. }
  42. precmd_functions+=(omz_termsupport_precmd)
  43. preexec_functions+=(omz_termsupport_preexec)
  44. # Runs before showing the prompt, to update the current directory in Terminal.app
  45. function omz_termsupport_cwd {
  46. # Notify Terminal.app of current directory using undocumented OSC sequence
  47. # found in OS X 10.9 and 10.10's /etc/bashrc
  48. if [[ $TERM_PROGRAM == Apple_Terminal ]] && [[ -z $INSIDE_EMACS ]]; then
  49. local PWD_URL="file://$HOSTNAME${PWD// /%20}"
  50. printf '\e]7;%s\a' "$PWD_URL"
  51. fi
  52. }
  53. precmd_functions+=(omz_termsupport_cwd)