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_dumb_terminal && return 91
8 [[ "$TERM" != "screen" && "$TERM" != "screen-256color" ]] || return 91
10 ITERM2_SHOULD_DECORATE_PROMPT="1"
12 # Indicates start of command output. Runs just before command executes.
13 iterm2_before_cmd_executes() {
14 printf "\033]133;C;\007"
17 iterm2_set_user_var() {
18 printf "\033]1337;SetUserVar=%s=%s\007" "$1" "$(printf "%s" "$2" | base64)"
21 # Users can write their own version of this method. It should call
22 # iterm2_set_user_var but not produce any other output.
23 # e.g., iterm2_set_user_var currentDirectory $PWD
24 # Accessible in iTerm2 (in a badge now, elsewhere in the future) as
25 # \(user.currentDirectory).
26 whence -v iterm2_print_user_vars > /dev/null 2>&1
27 if [[ $? -ne 0 ]]; then
28 iterm2_print_user_vars() {
33 iterm2_print_state_data() {
34 printf "\033]1337;RemoteHost=%s@%s\007" "$USER" "$iterm2_hostname"
35 printf "\033]1337;CurrentDir=%s\007" "$PWD"
36 iterm2_print_user_vars
39 # Report return code of command; runs after command finishes but before prompt
40 iterm2_after_cmd_executes() {
41 printf "\033]133;D;%s\007" "$STATUS"
42 iterm2_print_state_data
45 # Mark start of prompt
46 iterm2_prompt_start() {
47 printf "\033]133;A\007"
52 printf "\033]133;B\007"
55 # There are three possible paths in life.
57 # 1) A command is entered at the prompt and you press return.
58 # The following steps happen:
59 # * iterm2_preexec is invoked
60 # * PS1 is set to ITERM2_PRECMD_PS1
61 # * ITERM2_SHOULD_DECORATE_PROMPT is set to 1
62 # * The command executes (possibly reading or modifying PS1)
63 # * iterm2_precmd is invoked
64 # * ITERM2_PRECMD_PS1 is set to PS1 (as modified by command execution)
65 # * PS1 gets our escape sequences added to it
66 # * zsh displays your prompt
67 # * You start entering a command
69 # 2) You press ^C while entering a command at the prompt.
70 # The following steps happen:
71 # * (iterm2_preexec is NOT invoked)
72 # * iterm2_precmd is invoked
73 # * iterm2_before_cmd_executes is called since we detected that iterm2_preexec was not run
74 # * (ITERM2_PRECMD_PS1 and PS1 are not messed with, since PS1 already has our escape
75 # sequences and ITERM2_PRECMD_PS1 already has PS1's original value)
76 # * zsh displays your prompt
77 # * You start entering a command
79 # 3) A new shell is born.
80 # * PS1 has some initial value, either zsh's default or a value set before this script is sourced.
81 # * iterm2_precmd is invoked
82 # * ITERM2_SHOULD_DECORATE_PROMPT is initialized to 1
83 # * ITERM2_PRECMD_PS1 is set to the initial value of PS1
84 # * PS1 gets our escape sequences added to it
85 # * Your prompt is shown and you may begin entering a command.
88 # * ITERM2_SHOULD_DECORATE_PROMPT is 1 during and just after command execution, and "" while the prompt is
89 # shown and until you enter a command and press return.
90 # * PS1 does not have our escape sequences during command execution
91 # * After the command executes but before a new one begins, PS1 has escape sequences and
92 # ITERM2_PRECMD_PS1 has PS1's original value.
93 iterm2_decorate_prompt() {
94 # This should be a raw PS1 without iTerm2's stuff. It could be changed during command
96 ITERM2_PRECMD_PS1="$PS1"
97 ITERM2_SHOULD_DECORATE_PROMPT=""
99 # Add our escape sequences just before the prompt is shown.
100 PS1="%{$(iterm2_prompt_start)%}$PS1%{$(iterm2_prompt_end)%}"
105 if [ -z "$ITERM2_SHOULD_DECORATE_PROMPT" ]; then
106 # You pressed ^C while entering a command (iterm2_preexec did not run)
107 iterm2_before_cmd_executes
110 iterm2_after_cmd_executes "$STATUS"
112 if [ -n "$ITERM2_SHOULD_DECORATE_PROMPT" ]; then
113 iterm2_decorate_prompt
117 # This is not run if you press ^C while entering a command.
119 # Set PS1 back to its raw value prior to executing the command.
120 PS1="$ITERM2_PRECMD_PS1"
121 ITERM2_SHOULD_DECORATE_PROMPT="1"
122 iterm2_before_cmd_executes
125 # If hostname -f is slow on your system, set iterm2_hostname prior to sourcing this script.
126 [[ -z "$iterm2_hostname" ]] && iterm2_hostname=`hostname -f`
128 precmd_functions+=(iterm2_precmd)
129 preexec_functions+=(iterm2_preexec)
131 iterm2_print_state_data
132 printf "\033]1337;ShellIntegrationVersion=2;shell=zsh\007"