1 # AX-ZSH: Alex' Modular ZSH Configuration
2 # iterm2.zshrc: iTerm2 Shell Integration
4 [[ "$TERM_PROGRAM" = "iTerm.app" ]] || return
5 [[ "$TERM" != "screen" && "$TERM" != "screen-256color" ]] || return
7 # Indicates start of command output. Runs just before command executes.
8 iterm2_before_cmd_executes() {
9 printf "\033]133;C\007"
12 iterm2_set_user_var() {
13 printf "\033]1337;SetUserVar=%s=%s\007" "$1" $(printf "%s" "$2" | base64)
16 # Users can write their own version of this method. It should call
17 # iterm2_set_user_var but not produce any other output.
18 # e.g., iterm2_set_user_var currentDirectory $PWD
19 # Accessible in iTerm2 (in a badge now, elsewhere in the future) as
20 # \(user.currentDirectory).
21 iterm2_print_user_vars() {
24 iterm2_print_state_data() {
25 printf "\033]1337;RemoteHost=%s@%s\007" "$USER" "$iterm2_hostname"
26 printf "\033]1337;CurrentDir=%s\007" "$PWD"
27 iterm2_print_user_vars
30 # Report return code of command; runs after command finishes but before prompt
31 iterm2_after_cmd_executes() {
32 printf "\033]133;D;$?\007"
33 iterm2_print_state_data
36 # Mark start of prompt
37 iterm2_prompt_start() {
38 printf "\033]133;A\007"
43 printf "\033]133;B\007"
47 iterm2_after_cmd_executes
49 # The user or another precmd may have changed PS1 (e.g., powerline-shell).
50 # Ensure that our escape sequences are added back in.
51 if [[ "$ITERM2_SAVED_PS1" != "$PS1" ]]; then
52 PS1="%{$(iterm2_prompt_start)%}$PS1%{$(iterm2_prompt_end)%}"
53 ITERM2_SAVED_PS1="$PS1"
58 PS1="$ITERM2_SAVED_PS1"
59 iterm2_before_cmd_executes
62 # If hostname -f is slow on your system, set iterm2_hostname prior to sourcing this script.
63 [[ -z "$iterm2_hostname" ]] && iterm2_hostname=`hostname -f`
65 [[ -z $precmd_functions ]] && precmd_functions=()
66 precmd_functions+=(iterm2_precmd)
68 [[ -z $preexec_functions ]] && preexec_functions=()
69 preexec_functions+=(iterm2_preexec)
71 iterm2_print_state_data
72 printf "\033]1337;ShellIntegrationVersion=1\007"