Skip to content

Instantly share code, notes, and snippets.

@antifuchs
Last active July 15, 2022 15:15
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antifuchs/c8eca4bcb9d09a7bbbcd to your computer and use it in GitHub Desktop.
Save antifuchs/c8eca4bcb9d09a7bbbcd to your computer and use it in GitHub Desktop.
tmux-enabled iterm2 shell integration for zsh.
if [[ -o login ]]; then
TMUX_PREFIX=""
if [[ ! -z "$TMUX" ]] ; then
# Via
# <http://blog.yjl.im/2014/12/passing-escape-codes-for-changing-font.html>:
TMUX_PREFIX='\ePtmux;\e'
TMUX_POSTFIX='\e\\'
fi
# Indicates start of command output. Runs just before command executes.
iterm2_before_cmd_executes() {
printf "$TMUX_PREFIX\033]133;C\007$TMUX_POSTFIX"
}
iterm2_set_user_var() {
printf "$TMUX_PREFIX\033]1337;SetUserVar=%s=%s\007$TMUX_POSTFIX" "$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 "$TMUX_PREFIX\033]1337;RemoteHost=$USER@$iterm2_hostname\007$TMUX_POSTFIX"
printf "$TMUX_PREFIX\033]1337;CurrentDir=$PWD\007$TMUX_POSTFIX"
iterm2_print_user_vars
}
# Report return code of command; runs after command finishes but before prompt
iterm2_after_cmd_executes() {
printf "$TMUX_PREFIX\033]133;D;$?\007$TMUX_POSTFIX"
iterm2_print_state_data
}
# Mark start of prompt
iterm2_prompt_start() {
printf "$TMUX_PREFIX\033]133;A\007$TMUX_POSTFIX"
}
# Mark end of prompt
iterm2_prompt_end() {
printf "$TMUX_PREFIX\033]133;B\007$TMUX_POSTFIX"
}
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=($precmd_functions iterm2_precmd)
[[ -z $preexec_functions ]] && preexec_functions=()
preexec_functions=($preexec_functions iterm2_preexec)
iterm2_print_state_data
printf "$TMUX_PREFIX\033]1337;ShellIntegrationVersion=1\007"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment