Parent directory

iterm2.plugin.zsh

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