iterm2.plugin.zsh 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #####################################################
  2. # iTerm2 plugin for oh-my-zsh #
  3. # Author: Aviv Rosenberg (github.com/avivrosenberg) #
  4. #####################################################
  5. ###
  6. # This plugin is only relevant if the terminal is iTerm2 on OSX.
  7. if [[ "$OSTYPE" == darwin* ]] && [[ -n "$ITERM_SESSION_ID" ]] ; then
  8. # maybe make it the default in the future and allow opting out?
  9. if zstyle -t ':omz:plugins:iterm2' shell-integration; then
  10. # Handle $0 according to the standard:
  11. # https://zdharma-continuum.github.io/Zsh-100-Commits-Club/Zsh-Plugin-Standard.html
  12. 0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
  13. 0="${${(M)0:#/*}:-$PWD/$0}"
  14. # See official docs: https://iterm2.com/documentation-shell-integration.html
  15. source "${0:A:h}/iterm2_shell_integration.zsh"
  16. fi
  17. ###
  18. # Executes an arbitrary iTerm2 command via an escape code sequence.
  19. # See https://iterm2.com/documentation-escape-codes.html for all supported commands.
  20. # Example: $ _iterm2_command "1337;StealFocus"
  21. function _iterm2_command() {
  22. local cmd="$1"
  23. # Escape codes for wrapping commands for iTerm2.
  24. local iterm2_prefix="\x1B]"
  25. local iterm2_suffix="\x07"
  26. # If we're in tmux, a special escape code must be prepended/appended so that
  27. # the iTerm2 escape code is passed on into iTerm2.
  28. if [[ -n $TMUX ]]; then
  29. local tmux_prefix="\x1BPtmux;\x1B"
  30. local tmux_suffix="\x1B\\"
  31. fi
  32. echo -n "${tmux_prefix}${iterm2_prefix}${cmd}${iterm2_suffix}${tmux_suffix}"
  33. }
  34. ###
  35. # iterm2_profile(): Function for changing the current terminal window's
  36. # profile (colors, fonts, settings, etc).
  37. # To change the current iTerm2 profile, call this function and pass in a name
  38. # of another existing iTerm2 profile (name can contain spaces).
  39. function iterm2_profile() {
  40. # Desired name of profile
  41. local profile="$1"
  42. # iTerm2 command for changing profile
  43. local cmd="1337;SetProfile=$profile"
  44. # send the sequence
  45. _iterm2_command "${cmd}"
  46. # update shell variable
  47. ITERM_PROFILE="$profile"
  48. }
  49. ###
  50. # iterm2_tab_color(): Changes the color of iTerm2's currently active tab.
  51. # Usage: iterm2_tab_color <red> <green> <blue>
  52. # where red/green/blue are on the range 0-255.
  53. function iterm2_tab_color() {
  54. _iterm2_command "6;1;bg;red;brightness;$1"
  55. _iterm2_command "6;1;bg;green;brightness;$2"
  56. _iterm2_command "6;1;bg;blue;brightness;$3"
  57. }
  58. ###
  59. # iterm2_tab_color_reset(): Resets the color of iTerm2's current tab back to
  60. # default.
  61. function iterm2_tab_color_reset() {
  62. _iterm2_command "6;1;bg;*;default"
  63. }
  64. fi