1 # AX-ZSH: Alex' Modular ZSH Configuration
2 # iterm2.zshrc: iTerm2 Shell Integration
4 [[ -z "$AXZSH_PLUGIN_CHECK" ]] || return 92
6 # Check prerequisites ...
7 axzsh_is_modern_terminal || return 91
8 [[ -o interactive ]] || return 91
9 [[ -z "$ITERM_SHELL_INTEGRATION_INSTALLED" ]] || return 91
10 [[ "${ITERM_ENABLE_SHELL_INTEGRATION_WITH_TMUX-}$TERM" =~ "^screen" ]] && return 91
11 [[ "${ITERM_ENABLE_SHELL_INTEGRATION_WITH_TMUX-}$TERM" =~ "^tmux" ]] && return 91
13 # Add iTerm2 commands to PATH, when available.
14 [[ -d ~/.iterm2 ]] && path+=(~/.iterm2)
16 # Try to source user-local shell integration installed by iTerm2 itself,
17 # and only fall back to the implementation here when not found.
18 [[ -e "$HOME/.iterm2_shell_integration.zsh" ]] && source "$HOME/.iterm2_shell_integration.zsh"
20 # ax-zsh specific iTerm2 functions
21 iterm2_clear_captured_output() {
22 printf "\e]1337;ClearCapturedOutput\e\\"
25 [[ -z "$ITERM_SHELL_INTEGRATION_INSTALLED" ]] || return 0
27 ITERM_SHELL_INTEGRATION_INSTALLED="Yes"
28 ITERM2_SHOULD_DECORATE_PROMPT="1"
30 if [[ -n "$TMUX" ]]; then
31 # Pass escape sequences through in tmux(1), see
32 # <https://gist.github.com/antifuchs/c8eca4bcb9d09a7bbbcd>.
33 TMUX_PREFIX='\ePtmux;\e'
36 unset TMUX_PREFIX TMUX_POSTFIX
39 # Indicates start of command output. Runs just before command executes.
40 iterm2_before_cmd_executes() {
41 if [[ "$TERM_PROGRAM" = "iTerm.app" ]]; then
42 printf "${TMUX_PREFIX}\033]133;C;\r\007${TMUX_POSTFIX}"
44 printf "${TMUX_PREFIX}\033]133;C;\007${TMUX_POSTFIX}"
48 iterm2_set_user_var() {
49 printf "${TMUX_PREFIX}\033]1337;SetUserVar=%s=%s\007${TMUX_POSTFIX}" "$1" "$(printf "%s" "$2" | base64 | tr -d '\n')"
52 # Users can write their own version of this method. It should call
53 # iterm2_set_user_var but not produce any other output.
54 # e.g., iterm2_set_user_var currentDirectory $PWD
55 # Accessible in iTerm2 (in a badge now, elsewhere in the future) as
56 # \(user.currentDirectory).
57 if ! whence iterm2_print_user_vars >/dev/null; then
58 iterm2_print_user_vars() {
63 iterm2_print_state_data() {
64 printf "${TMUX_PREFIX}\033]1337;RemoteHost=%s@%s\007${TMUX_POSTFIX}" "$USER" "$HOST"
65 printf "${TMUX_PREFIX}\033]1337;CurrentDir=%s\007${TMUX_POSTFIX}" "$PWD"
66 iterm2_print_user_vars
69 # Report return code of command; runs after command finishes but before prompt
70 iterm2_after_cmd_executes() {
71 printf "${TMUX_PREFIX}\033]133;D;%s\007${TMUX_POSTFIX}" "$STATUS"
72 iterm2_print_state_data
75 # Mark start of prompt
76 iterm2_prompt_mark() {
77 printf "${TMUX_PREFIX}\033]133;A\007${TMUX_POSTFIX}"
82 printf "${TMUX_PREFIX}\033]133;B\007${TMUX_POSTFIX}"
85 # There are three possible paths in life.
87 # 1) A command is entered at the prompt and you press return.
88 # The following steps happen:
89 # * iterm2_preexec is invoked
90 # * PS1 is set to ITERM2_PRECMD_PS1
91 # * ITERM2_SHOULD_DECORATE_PROMPT is set to 1
92 # * The command executes (possibly reading or modifying PS1)
93 # * iterm2_precmd is invoked
94 # * ITERM2_PRECMD_PS1 is set to PS1 (as modified by command execution)
95 # * PS1 gets our escape sequences added to it
96 # * zsh displays your prompt
97 # * You start entering a command
99 # 2) You press ^C while entering a command at the prompt.
100 # The following steps happen:
101 # * (iterm2_preexec is NOT invoked)
102 # * iterm2_precmd is invoked
103 # * iterm2_before_cmd_executes is called since we detected that iterm2_preexec was not run
104 # * (ITERM2_PRECMD_PS1 and PS1 are not messed with, since PS1 already has our escape
105 # sequences and ITERM2_PRECMD_PS1 already has PS1's original value)
106 # * zsh displays your prompt
107 # * You start entering a command
109 # 3) A new shell is born.
110 # * PS1 has some initial value, either zsh's default or a value set before this script is sourced.
111 # * iterm2_precmd is invoked
112 # * ITERM2_SHOULD_DECORATE_PROMPT is initialized to 1
113 # * ITERM2_PRECMD_PS1 is set to the initial value of PS1
114 # * PS1 gets our escape sequences added to it
115 # * Your prompt is shown and you may begin entering a command.
118 # * ITERM2_SHOULD_DECORATE_PROMPT is 1 during and just after command execution, and "" while the prompt is
119 # shown and until you enter a command and press return.
120 # * PS1 does not have our escape sequences during command execution
121 # * After the command executes but before a new one begins, PS1 has escape sequences and
122 # ITERM2_PRECMD_PS1 has PS1's original value.
123 iterm2_decorate_prompt() {
124 # This should be a raw PS1 without iTerm2's stuff. It could be changed during command
126 ITERM2_PRECMD_PS1="$PS1"
127 ITERM2_SHOULD_DECORATE_PROMPT=""
129 # Add our escape sequences just before the prompt is shown.
130 # Use ITERM2_SQUELCH_MARK for people who can't modify PS1 directly, like powerlevel9k users.
131 # This is gross but I had a heck of a time writing a correct if statement for zsh 5.0.2.
133 if [[ $PS1 == *"$(iterm2_prompt_mark)"* ]]; then
135 elif [[ "${ITERM2_SQUELCH_MARK-}" != "" ]]; then
138 PREFIX="%{$(iterm2_prompt_mark)%}"
140 PS1="$PREFIX$PS1%{$(iterm2_prompt_end)%}"
141 ITERM2_DECORATED_PS1="$PS1"
146 if [[ -z "${ITERM2_SHOULD_DECORATE_PROMPT-}" ]]; then
147 # You pressed ^C while entering a command (iterm2_preexec did not run)
148 iterm2_before_cmd_executes
149 if [[ "$PS1" != "${ITERM2_DECORATED_PS1-}" ]]; then
150 # PS1 changed, perhaps in another precmd. See issue 9938.
151 ITERM2_SHOULD_DECORATE_PROMPT="1"
155 iterm2_after_cmd_executes "$STATUS"
157 if [[ -n "$ITERM2_SHOULD_DECORATE_PROMPT" ]]; then
158 iterm2_decorate_prompt
162 # This is not run if you press ^C while entering a command.
164 # Set PS1 back to its raw value prior to executing the command.
165 PS1="$ITERM2_PRECMD_PS1"
166 ITERM2_SHOULD_DECORATE_PROMPT="1"
167 iterm2_before_cmd_executes
170 precmd_functions+=(iterm2_precmd)
171 preexec_functions+=(iterm2_preexec)
173 iterm2_print_state_data
174 printf "${TMUX_PREFIX}\033]1337;ShellIntegrationVersion=14;shell=zsh\007${TMUX_POSTFIX}"