From a450b3cb45b38c9b4f3c6cfbac56dab32f580ec5 Mon Sep 17 00:00:00 2001 From: "Kenneth Benzie (Benie)" Date: Tue, 2 Jan 2018 13:36:47 +0000 Subject: [PATCH] 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. --- zshrc | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/zshrc b/zshrc index 1ed03b0..9fe4c58 100644 --- a/zshrc +++ b/zshrc @@ -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