Fix cursor shape change bug in tmux

Sending escape sequences directly from zsh and bypassing tmux results in
the wrong cursor shape when switching between panes which no longer
reflect the current zsh vi mode.

When using ZTE compatible terminals tmux supports tracking cursor shape
changes on a per pane basis using a suitable `terminal-override`, this
allows zsh and vim to behave as if they are not operating in tmux and
everything works as expected. However, so far I've not been able to
reproduce this behaviour in iTerm2.
This commit is contained in:
Kenneth Benzie 2018-01-02 13:36:47 +00:00 committed by Kenneth Benzie (Benie)
parent 2846e7d68e
commit a450b3cb45

28
zshrc
View File

@ -77,21 +77,23 @@ autoload -U edit-command-line
zle -N edit-command-line
bindkey -M vicmd '^V' edit-command-line
# Get the shells parent process name.
ppid_name() { echo $(ps -p $(ps -p $(echo $$) -o ppid=) -o comm=) }
# Enable changing cursor shape based on vi mode
if [ "$TMUX" != "" ]; then
tmux_before="\EPtmux;\E"
tmux_after="\E\\"
if [ "$ITERM_PROFILE" != "" ]; then
# TODO: This only works outside tmux, unlike VTE compatible terminals I've
# not found a tmux terminal-override which works with iTerm2's CursorShape
# escape sequences.
# iTerm2 cursor shape escape sequences
cursor_block="\E]50;CursorShape=0\C-G"
cursor_line="\E]50;CursorShape=1\C-G"
elif [ "$(ppid_name)" != "python2" ]; then
# VTE compatible cursor shape escape sequences, excluding Guake even though
# it is VTE based it doesn't seem to handle these properly
cursor_block="\e[2 q"
cursor_line="\e[6 q"
fi
if [ "$ITERM_SESSION_ID" != "" ]; then
# iterm2 cursor shape escape sequences
cursor_block="$tmux_before\E]50;CursorShape=0\C-G$tmux_after"
cursor_line="$tmux_before\E]50;CursorShape=1\C-G$tmux_after"
else
# VTE compatible cursor shape escape sequences
cursor_block="$tmux_before\E[2 q$tmux_after"
cursor_line="$tmux_before\E[6 q$tmux_after"
fi
unset tmux_before tmux_after
if [[ ! -z "$cursor_block" && ! -z "$cursor_line" ]]; then
# Change cursor shape when vi mode changes