Skip to content

Instantly share code, notes, and snippets.

@andyfowler
Created September 5, 2011 18:08
Show Gist options
  • Select an option

  • Save andyfowler/1195581 to your computer and use it in GitHub Desktop.

Select an option

Save andyfowler/1195581 to your computer and use it in GitHub Desktop.
Swap iTerm2 cursors in vim insert mode when using tmux
" tmux will only forward escape sequences to the terminal if surrounded by a DCS sequence
" http://sourceforge.net/mailarchive/forum.php?thread_name=AANLkTinkbdoZ8eNR1X2UobLTeww1jFrvfJxTMfKSq-L%2B%40mail.gmail.com&forum_name=tmux-users
if exists('$TMUX')
let &t_SI = "\<Esc>Ptmux;\<Esc>\<Esc>]50;CursorShape=1\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_EI = "\<Esc>]50;CursorShape=0\x7"
endif
@HelloGrayson

Copy link
Copy Markdown

+1 to NVIM_TUI_ENABLE_CURSOR_SHAPE - just add it to your ~/.config/nvim/init.vim like so:

:let $NVIM_TUI_ENABLE_CURSOR_SHAPE=1

@HelloGrayson

Copy link
Copy Markdown

Wondering if the same thing can be achieved in ZSH...

I have something working in zsh/iterm, but not in tmux:

function zle-keymap-select zle-line-init
{
    case $KEYMAP in
        vicmd) # block cursor
            print -n -- "\E]50;CursorShape=0\C-G"
            ;;
        viins|main) # line cursor
            print -n -- "\E]50;CursorShape=1\C-G"
            fi
            ;;
    esac
    zle reset-prompt
    zle -R
}
function zle-line-finish
{
    print -n -- "\E]50;CursorShape=0\C-G"
}
zle -N zle-line-init
zle -N zle-line-finish
zle -N zle-keymap-select

Any help getting the above working in TMUX/ZSH would be greatly appreciated.

@jcorbin

jcorbin commented Feb 2, 2016

Copy link
Copy Markdown

@breerly here's an adapted copy of your functions that work under tmux using the DCS escape mentioned by OP: https://github.com/jcorbin/home/blob/master/.zsh/rc.d/vi-mode-cursor

@HelloGrayson

Copy link
Copy Markdown

@jcorbin looks like it works, I'll try it on for awhile 👍

@silesky

silesky commented Aug 17, 2016

Copy link
Copy Markdown

@breerly here's an adapted copy of your functions that work under tmux using the DCS escape mentioned by OP: https://github.com/jcorbin/home/blob/master/.zsh/rc.d/vi-mode-cursor

@jcorbin ^ works on osx w/ zshell + tmux! thanks a lot! 👍

@silesky

silesky commented Aug 19, 2016

Copy link
Copy Markdown

@jcorbin I spoke too soon: it works well until you open up another pane. Switching between the two means that the cursor get's carried along, even if the pane you're switching to is in command mode. This leads to situations where you get the block cursor but you're actually in insert mode, and vice versa.

@dlee

dlee commented Dec 8, 2016

Copy link
Copy Markdown

@silesky, were you able to find a workaround for pane switching?

@rawaludin

Copy link
Copy Markdown

Thanks @jcorbin

@nanoguo

nanoguo commented Feb 7, 2017

Copy link
Copy Markdown

thanks

@resoliwan

Copy link
Copy Markdown

Thanks!

@ddickstein

Copy link
Copy Markdown

How can I get this working in the case of running ssh and opening vim on a remote machine from within a local tmux session?

https://stackoverflow.com/questions/42377945/vim-adding-cursorshape-support-over-tmux-ssh

@orklann

orklann commented Apr 17, 2017

Copy link
Copy Markdown

@AdamBrodzinski
Try adding this line to your .vimrc to get rid of the delay while switching between normal mode and insert mode

set timeoutlen=1000 ttimeoutlen=0

ghost commented Aug 27, 2017

Copy link
Copy Markdown

Thanks!

@joshperry

Copy link
Copy Markdown

We should all get together for a drink some day.

@minawk

minawk commented Aug 18, 2018

Copy link
Copy Markdown

Thanks!

@luong-komorebi

Copy link
Copy Markdown

A note if you are using tmux with a custom term-info (like many tutorials in the internet instructs you to have something like - italic in vim)
This is what you want inside your tmux.conf (assuming using iterm2 + tmux)

set -g default-terminal 'tmux-256color' # replace this with your custom term-info name
set -ga terminal-overrides ',*:Tc' # this is for 256 color
set -ga terminal-overrides '*:Ss=\E[%p1%d q:Se=\E[ q' # this is for the cursor shape

@Yossarian0916

Copy link
Copy Markdown

won't work for tmux 2.6 and terminator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment