]> arthur.barton.de Git - ax-zsh.git/blob - plugins/iterm2/iterm2.zshrc
Add "iterm2" plugin: iTerm2.app shell integration
[ax-zsh.git] / plugins / iterm2 / iterm2.zshrc
1 # AX-ZSH: Alex' Modular ZSH Configuration
2 # iterm2.zshrc: iTerm2 Shell Integration
3
4 [[ "$TERM_PROGRAM" = "iTerm.app" ]] || return
5 [[ "$TERM" != "screen" && "$TERM" != "screen-256color" ]] || return
6
7 # Indicates start of command output. Runs just before command executes.
8 iterm2_before_cmd_executes() {
9         printf "\033]133;C\007"
10 }
11
12 iterm2_set_user_var() {
13         printf "\033]1337;SetUserVar=%s=%s\007" "$1" $(printf "%s" "$2" | base64)
14 }
15
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() {
22 }
23
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
28 }
29
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
34 }
35
36 # Mark start of prompt
37 iterm2_prompt_start() {
38         printf "\033]133;A\007"
39 }
40
41 # Mark end of prompt
42 iterm2_prompt_end() {
43         printf "\033]133;B\007"
44 }
45
46 iterm2_precmd() {
47         iterm2_after_cmd_executes
48
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"
54         fi
55 }
56
57 iterm2_preexec() {
58         PS1="$ITERM2_SAVED_PS1"
59         iterm2_before_cmd_executes
60 }
61
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`
64
65 [[ -z $precmd_functions ]] && precmd_functions=()
66 precmd_functions+=(iterm2_precmd)
67
68 [[ -z $preexec_functions ]] && preexec_functions=()
69 preexec_functions+=(iterm2_preexec)
70
71 iterm2_print_state_data
72 printf "\033]1337;ShellIntegrationVersion=1\007"