520
+ − 1 # This program is free software; you can redistribute it and/or
+ − 2 # modify it under the terms of the GNU General Public License
+ − 3 # as published by the Free Software Foundation; either version 2
+ − 4 # of the License, or (at your option) any later version.
+ − 5 #
+ − 6 # This program is distributed in the hope that it will be useful,
+ − 7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
+ − 8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ − 9 # GNU General Public License for more details.
+ − 10 #
+ − 11 # You should have received a copy of the GNU General Public License
+ − 12 # along with this program; if not, write to the Free Software
+ − 13 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ − 14
+ − 15 if [[ -o interactive ]] ; then
+ − 16 if [ " ${ ITERM_ENABLE_SHELL_INTEGRATION_WITH_TMUX - } "" $TERM " != "screen" -a " ${ ITERM_SHELL_INTEGRATION_INSTALLED - } " = "" -a " $TERM " != linux -a " $TERM " != dumb ] ; then
+ − 17 ITERM_SHELL_INTEGRATION_INSTALLED = Yes
+ − 18 ITERM2_SHOULD_DECORATE_PROMPT = "1"
+ − 19 # Indicates start of command output. Runs just before command executes.
+ − 20 iterm2_before_cmd_executes() {
+ − 21 if [ " $TERM_PROGRAM " = "iTerm.app" ] ; then
+ − 22 printf "\033]133;C;\r\007"
+ − 23 else
+ − 24 printf "\033]133;C;\007"
+ − 25 fi
+ − 26 }
+ − 27
+ − 28 iterm2_set_user_var() {
+ − 29 printf "\033]1337;SetUserVar=%s=%s\007" " $1 " $( printf "%s" " $2 " | base64 | tr -d '\n' )
+ − 30 }
+ − 31
+ − 32 # Users can write their own version of this method. It should call
+ − 33 # iterm2_set_user_var but not produce any other output.
+ − 34 # e.g., iterm2_set_user_var currentDirectory $PWD
+ − 35 # Accessible in iTerm2 (in a badge now, elsewhere in the future) as
+ − 36 # \(user.currentDirectory).
+ − 37 whence -v iterm2_print_user_vars > /dev/null 2 >& 1
+ − 38 if [ $? -ne 0 ] ; then
+ − 39 iterm2_print_user_vars() {
+ − 40 true
+ − 41 }
+ − 42 fi
+ − 43
+ − 44 iterm2_print_state_data() {
+ − 45 local _iterm2_hostname = " ${ iterm2_hostname - } "
+ − 46 if [ -z " ${ iterm2_hostname :- } " ] ; then
+ − 47 _iterm2_hostname = $( hostname -f 2 >/dev/null)
+ − 48 fi
+ − 49 printf "\033]1337;RemoteHost=%s@%s\007" " $USER " " ${ _iterm2_hostname - } "
+ − 50 printf "\033]1337;CurrentDir=%s\007" " $PWD "
+ − 51 iterm2_print_user_vars
+ − 52 }
+ − 53
+ − 54 # Report return code of command; runs after command finishes but before prompt
+ − 55 iterm2_after_cmd_executes() {
+ − 56 printf "\033]133;D;%s\007" " $STATUS "
+ − 57 iterm2_print_state_data
+ − 58 }
+ − 59
+ − 60 # Mark start of prompt
+ − 61 iterm2_prompt_mark() {
+ − 62 printf "\033]133;A\007"
+ − 63 }
+ − 64
+ − 65 # Mark end of prompt
+ − 66 iterm2_prompt_end() {
+ − 67 printf "\033]133;B\007"
+ − 68 }
+ − 69
+ − 70 # There are three possible paths in life.
+ − 71 #
+ − 72 # 1) A command is entered at the prompt and you press return.
+ − 73 # The following steps happen:
+ − 74 # * iterm2_preexec is invoked
+ − 75 # * PS1 is set to ITERM2_PRECMD_PS1
+ − 76 # * ITERM2_SHOULD_DECORATE_PROMPT is set to 1
+ − 77 # * The command executes (possibly reading or modifying PS1)
+ − 78 # * iterm2_precmd is invoked
+ − 79 # * ITERM2_PRECMD_PS1 is set to PS1 (as modified by command execution)
+ − 80 # * PS1 gets our escape sequences added to it
+ − 81 # * zsh displays your prompt
+ − 82 # * You start entering a command
+ − 83 #
+ − 84 # 2) You press ^C while entering a command at the prompt.
+ − 85 # The following steps happen:
+ − 86 # * (iterm2_preexec is NOT invoked)
+ − 87 # * iterm2_precmd is invoked
+ − 88 # * iterm2_before_cmd_executes is called since we detected that iterm2_preexec was not run
+ − 89 # * (ITERM2_PRECMD_PS1 and PS1 are not messed with, since PS1 already has our escape
+ − 90 # sequences and ITERM2_PRECMD_PS1 already has PS1's original value)
+ − 91 # * zsh displays your prompt
+ − 92 # * You start entering a command
+ − 93 #
+ − 94 # 3) A new shell is born.
+ − 95 # * PS1 has some initial value, either zsh's default or a value set before this script is sourced.
+ − 96 # * iterm2_precmd is invoked
+ − 97 # * ITERM2_SHOULD_DECORATE_PROMPT is initialized to 1
+ − 98 # * ITERM2_PRECMD_PS1 is set to the initial value of PS1
+ − 99 # * PS1 gets our escape sequences added to it
+ − 100 # * Your prompt is shown and you may begin entering a command.
+ − 101 #
+ − 102 # Invariants:
+ − 103 # * ITERM2_SHOULD_DECORATE_PROMPT is 1 during and just after command execution, and "" while the prompt is
+ − 104 # shown and until you enter a command and press return.
+ − 105 # * PS1 does not have our escape sequences during command execution
+ − 106 # * After the command executes but before a new one begins, PS1 has escape sequences and
+ − 107 # ITERM2_PRECMD_PS1 has PS1's original value.
+ − 108 iterm2_decorate_prompt() {
+ − 109 # This should be a raw PS1 without iTerm2's stuff. It could be changed during command
+ − 110 # execution.
+ − 111 ITERM2_PRECMD_PS1 = " $PS1 "
+ − 112 ITERM2_SHOULD_DECORATE_PROMPT = ""
+ − 113
+ − 114 # Add our escape sequences just before the prompt is shown.
+ − 115 # Use ITERM2_SQUELCH_MARK for people who can't mdoify PS1 directly, like powerlevel9k users.
+ − 116 # This is gross but I had a heck of a time writing a correct if statetment for zsh 5.0.2.
+ − 117 local PREFIX = ""
+ − 118 if [[ $PS1 == *" $( iterm2_prompt_mark) " * ]] ; then
+ − 119 PREFIX = ""
+ − 120 elif [[ " ${ ITERM2_SQUELCH_MARK - } " != "" ]] ; then
+ − 121 PREFIX = ""
+ − 122 else
+ − 123 PREFIX = "%{ $( iterm2_prompt_mark) %}"
+ − 124 fi
+ − 125 PS1 = " $PREFIX$PS1 %{ $( iterm2_prompt_end) %}"
+ − 126 ITERM2_DECORATED_PS1 = " $PS1 "
+ − 127 }
+ − 128
+ − 129 iterm2_precmd() {
+ − 130 local STATUS = " $? "
+ − 131 if [ -z " ${ ITERM2_SHOULD_DECORATE_PROMPT - } " ] ; then
+ − 132 # You pressed ^C while entering a command (iterm2_preexec did not run)
+ − 133 iterm2_before_cmd_executes
+ − 134 if [ " $PS1 " != " ${ ITERM2_DECORATED_PS1 - } " ] ; then
+ − 135 # PS1 changed, perhaps in another precmd. See issue 9938.
+ − 136 ITERM2_SHOULD_DECORATE_PROMPT = "1"
+ − 137 fi
+ − 138 fi
+ − 139
+ − 140 iterm2_after_cmd_executes " $STATUS "
+ − 141
+ − 142 if [ -n " $ITERM2_SHOULD_DECORATE_PROMPT " ] ; then
+ − 143 iterm2_decorate_prompt
+ − 144 fi
+ − 145 }
+ − 146
+ − 147 # This is not run if you press ^C while entering a command.
+ − 148 iterm2_preexec() {
+ − 149 # Set PS1 back to its raw value prior to executing the command.
+ − 150 PS1 = " $ITERM2_PRECMD_PS1 "
+ − 151 ITERM2_SHOULD_DECORATE_PROMPT = "1"
+ − 152 iterm2_before_cmd_executes
+ − 153 }
+ − 154
+ − 155 # If hostname -f is slow on your system set iterm2_hostname prior to
+ − 156 # sourcing this script. We know it is fast on macOS so we don't cache
+ − 157 # it. That lets us handle the hostname changing like when you attach
+ − 158 # to a VPN.
+ − 159 if [ -z " ${ iterm2_hostname - } " ] ; then
+ − 160 if [ " $( uname) " != "Darwin" ] ; then
+ − 161 iterm2_hostname = ` hostname -f 2 >/dev/null`
+ − 162 # Some flavors of BSD (i.e. NetBSD and OpenBSD) don't have the -f option.
+ − 163 if [ $? -ne 0 ] ; then
+ − 164 iterm2_hostname = ` hostname`
+ − 165 fi
+ − 166 fi
+ − 167 fi
+ − 168
+ − 169 [[ -z ${ precmd_functions - } ]] && precmd_functions =()
+ − 170 precmd_functions =( $precmd_functions iterm2_precmd)
+ − 171
+ − 172 [[ -z ${ preexec_functions - } ]] && preexec_functions =()
+ − 173 preexec_functions =( $preexec_functions iterm2_preexec)
+ − 174
+ − 175 iterm2_print_state_data
+ − 176 printf "\033]1337;ShellIntegrationVersion=13;shell=zsh\007"
+ − 177 fi
+ − 178 fi