vim change cursor in different modes with tmux and iTerm2 - works on ssh but not on local machine?

631 views Asked by At

I am trying to make the cursor change depending on the mode in vim while in tmux (using iTerm2 as my terminal emulator). The below setup in my .vimrc works on my local machine without being in tmux, as well as when I ssh and use tmux, but not when I use tmux on my local machine. The setup (see below) is from here: https://vim.fandom.com/wiki/Change_cursor_shape_in_different_modes

if exists('$TMUX')
    let &t_SI = "\<Esc>Ptmux;\<Esc>\<Esc>]50;CursorShape=1\x7\<Esc>\\"
       let &t_SR =  "\<Esc>Ptmux;\<Esc>\<Esc>]50;CursorShape=2\x7\<Esc>\\"
         let &t_EI = "\<Esc>Ptmux;\<Esc>\<Esc>]50;CursorShape=0\x7\<Esc>\\"
     else
           let &t_SI = "\<Esc>]50;CursorShape=1\x7"
             let &t_SR = "\<Esc>]50;CursorShape=2\x7"
               let &t_EI = "\<Esc>]50;CursorShape=0\x7"
         endif
:autocmd InsertEnter,InsertLeave * set cul!
1

There are 1 answers

0
Victor On

This worked for me (iTerm2 v3.4.16, Tmux v3.2, Vim v9.0):

" Change cursor shape on different mode
if empty($TMUX)
  " Vertical bar in insert mode
  let &t_SI = "\<Esc>]50;CursorShape=1\x7"
  " Block in normal mode
  let &t_EI = "\<Esc>]50;CursorShape=0\x7"
  " Underline in replace mode
  let &t_SR = "\<Esc>]50;CursorShape=2\x7"
else
  " Vertical bar in insert mode
  let &t_SI = "\e[5 q"
  " Block in normal mode
  let &t_SR = "\e[4 q"
  " Underline in replace mode
  let &t_EI = "\e[1 q"
endif

For some reasons, swapping the logic to use exists($TMUX) doesn't work.