termsupport.zsh 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #usage: title short_tab_title looooooooooooooooooooooggggggg_windows_title
  2. #http://www.faqs.org/docs/Linux-mini/Xterm-Title.html#ss3.1
  3. #Fully support screen, iterm, and probably most modern xterm and rxvt
  4. function title {
  5. if [[ "$DISABLE_AUTO_TITLE" == "true" ]] || [[ "$EMACS" == *term* ]]; then
  6. return
  7. fi
  8. if [[ "$TERM" == screen* ]]; then
  9. print -Pn "\ek$1:q\e\\" #set screen hardstatus, usually truncated at 20 chars
  10. elif [[ "$TERM" == xterm* ]] || [[ $TERM == rxvt* ]] || [[ $TERM == ansi ]] || [[ "$TERM_PROGRAM" == "iTerm.app" ]]; then
  11. print -Pn "\e]2;$2:q\a" #set window name
  12. print -Pn "\e]1;$1:q\a" #set icon (=tab) name
  13. fi
  14. }
  15. ZSH_THEME_TERM_TAB_TITLE_IDLE="%15<..<%~%<<" #15 char left truncated PWD
  16. ZSH_THEME_TERM_TITLE_IDLE="%n@%m: %~"
  17. #Appears when you have the prompt
  18. function omz_termsupport_precmd {
  19. title $ZSH_THEME_TERM_TAB_TITLE_IDLE $ZSH_THEME_TERM_TITLE_IDLE
  20. }
  21. #Appears at the beginning of (and during) of command execution
  22. function omz_termsupport_preexec {
  23. emulate -L zsh
  24. setopt extended_glob
  25. # cmd name only, or if this is sudo or ssh, the next cmd
  26. local CMD=${1[(wr)^(*=*|sudo|ssh|rake|-*)]:gs/%/%%}
  27. local LINE="${2:gs/%/%%}"
  28. title '$CMD' '%100>...>$LINE%<<'
  29. }
  30. #Appears each time pwd is changed
  31. function omz_termsupport_chpwd {
  32. #Notify Terminal.app of current directory using undocumented OSC sequence
  33. #found in OS X 10.10's /etc/bashrc
  34. if [[ $TERM_PROGRAM == Apple_Terminal ]] && [[ -z $INSIDE_EMACS ]]; then
  35. local PWD_URL="file://$HOSTNAME${PWD// /%20}"
  36. printf '\e]7;%s\a' "$PWD_URL"
  37. fi
  38. }
  39. #Fire it once so the pwd is set properly upon shell startup
  40. omz_termsupport_chpwd
  41. precmd_functions+=(omz_termsupport_precmd)
  42. preexec_functions+=(omz_termsupport_preexec)
  43. chpwd_functions+=(omz_termsupport_chpwd)