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!
This worked for me (iTerm2 v3.4.16, Tmux v3.2, Vim v9.0):
For some reasons, swapping the logic to use
exists($TMUX)doesn't work.