How to change the cursor between Normal and Insert modes in Vim?

I would like to know how to change, if possible, the cursor in Vim (in color, shape, etc.) depending on what mode you are in.

I am constantly forgetting that I am not in Insert mode and start typing code, which results in all sorts of crazy things happening. It would be helpful if there was some sort of visual indication on the cursor.


A popular approach to indicate switching to and from Insert mode is toggling the cursorline option, which is responsible for whether the current screen line is highlighted (see :help cursorline):

:autocmd InsertEnter,InsertLeave * set cul!

or, alternatively:

:autocmd InsertEnter * set cul
:autocmd InsertLeave * set nocul

Modify the CursorLine highlighting group to change the styling of the cursor line to your liking (see :help :highlight and :help highlight-groups).


The following works in xterm, urxvt, and other terminal emulators on Linux; iTerm2 on macOS; Git Bash with ConEmu on Windows; and more (see comments):

let &t_SI = "\e[6 q"
let &t_EI = "\e[2 q"

" reset the cursor on start (for older versions of vim, usually not required)
augroup myCmds
au!
autocmd VimEnter * silent !echo -ne "\e[2 q"
augroup END

Other options (replace the number after \e[):

Ps = 0  -> blinking block.
Ps = 1  -> blinking block (default).
Ps = 2  -> steady block.
Ps = 3  -> blinking underline.
Ps = 4  -> steady underline.
Ps = 5  -> blinking bar (xterm).
Ps = 6  -> steady bar (xterm).

When you use tmux, it is important to use it like that (without the \<Esc>Ptmux; escape). tmux will keep track of the correct cursor shape when you switch windows/panes.

If it does not work for you, try either to set TERM=xterm-256color before starting tmux, or add this to your .tmux.conf (thanks @Steven Lu):

set -ga terminal-overrides ',*:Ss=\E[%p1%d q:Se=\E[2 q'

If you are using tmux and iTerm2 on macOS,
the following changes the cursor from a block to a cursor and highlights the current line

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
:autocmd InsertEnter * set cul
:autocmd InsertLeave * set nocul

credit: https://gist.github.com/andyfowler/1195581