]> arthur.barton.de Git - ax-zsh.git/commitdiff
Add "iterm2" plugin: iTerm2.app shell integration
authorAlexander Barton <alex@barton.de>
Tue, 29 Sep 2015 11:35:00 +0000 (13:35 +0200)
committerAlexander Barton <alex@barton.de>
Tue, 29 Sep 2015 11:35:00 +0000 (13:35 +0200)
plugins/iterm2/README.md [new file with mode: 0644]
plugins/iterm2/iterm2.zshrc [new file with mode: 0644]

diff --git a/plugins/iterm2/README.md b/plugins/iterm2/README.md
new file mode 100644 (file)
index 0000000..8931c69
--- /dev/null
@@ -0,0 +1,8 @@
+## iterm2
+
+*iTerm2* (Terminal application for OS X) Shell Integration, see
+<https://iterm2.com/shell_integration.html> for details.
+
+### Prerequisites
+
+- iTerm 2, version 2.9 (or newer).
diff --git a/plugins/iterm2/iterm2.zshrc b/plugins/iterm2/iterm2.zshrc
new file mode 100644 (file)
index 0000000..8f50698
--- /dev/null
@@ -0,0 +1,72 @@
+# AX-ZSH: Alex' Modular ZSH Configuration
+# iterm2.zshrc: iTerm2 Shell Integration
+
+[[ "$TERM_PROGRAM" = "iTerm.app" ]] || return
+[[ "$TERM" != "screen" && "$TERM" != "screen-256color" ]] || return
+
+# Indicates start of command output. Runs just before command executes.
+iterm2_before_cmd_executes() {
+       printf "\033]133;C\007"
+}
+
+iterm2_set_user_var() {
+       printf "\033]1337;SetUserVar=%s=%s\007" "$1" $(printf "%s" "$2" | base64)
+}
+
+# Users can write their own version of this method. It should call
+# iterm2_set_user_var but not produce any other output.
+# e.g., iterm2_set_user_var currentDirectory $PWD
+# Accessible in iTerm2 (in a badge now, elsewhere in the future) as
+# \(user.currentDirectory).
+iterm2_print_user_vars() {
+}
+
+iterm2_print_state_data() {
+       printf "\033]1337;RemoteHost=%s@%s\007" "$USER" "$iterm2_hostname"
+       printf "\033]1337;CurrentDir=%s\007" "$PWD"
+       iterm2_print_user_vars
+}
+
+# Report return code of command; runs after command finishes but before prompt
+iterm2_after_cmd_executes() {
+       printf "\033]133;D;$?\007"
+       iterm2_print_state_data
+}
+
+# Mark start of prompt
+iterm2_prompt_start() {
+       printf "\033]133;A\007"
+}
+
+# Mark end of prompt
+iterm2_prompt_end() {
+       printf "\033]133;B\007"
+}
+
+iterm2_precmd() {
+       iterm2_after_cmd_executes
+
+       # The user or another precmd may have changed PS1 (e.g., powerline-shell).
+       # Ensure that our escape sequences are added back in.
+       if [[ "$ITERM2_SAVED_PS1" != "$PS1" ]]; then
+               PS1="%{$(iterm2_prompt_start)%}$PS1%{$(iterm2_prompt_end)%}"
+               ITERM2_SAVED_PS1="$PS1"
+       fi
+}
+
+iterm2_preexec() {
+       PS1="$ITERM2_SAVED_PS1"
+       iterm2_before_cmd_executes
+}
+
+# If hostname -f is slow on your system, set iterm2_hostname prior to sourcing this script.
+[[ -z "$iterm2_hostname" ]] && iterm2_hostname=`hostname -f`
+
+[[ -z $precmd_functions ]] && precmd_functions=()
+precmd_functions+=(iterm2_precmd)
+
+[[ -z $preexec_functions ]] && preexec_functions=()
+preexec_functions+=(iterm2_preexec)
+
+iterm2_print_state_data
+printf "\033]1337;ShellIntegrationVersion=1\007"