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

Vim

Vim Problem Overview


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.

Vim Solutions


Solution 1 - Vim

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).

Solution 2 - Vim

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'

Solution 3 - Vim

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

Solution 4 - Vim

Not sure if anyone else is facing a delay after hitting the Esc key to go back to normal mode to show the block cursor but if so, this is the way I fix it too.

Actually I'm using iTerm2 and using Vim inside my terminal on macOS. And when entering to insert mode, the cursor still being a block and is kind of confusing when you are at insert mode or normal mode.

I wanted to show a thin line as cursor when in insert mode and back to block when in normal mode as MacVim does. And to do so it's pretty simple, just added this to my .vimrc file as described here:

let &t_SI = "\<Esc>]50;CursorShape=1\x7"
let &t_SR = "\<Esc>]50;CursorShape=2\x7"
let &t_EI = "\<Esc>]50;CursorShape=0\x7"

enter image description here

But as you can see there was a delay when hitting ESC to exit insert mode back to normal mode and show the block as cursor again. So to fix it I found this:

set ttimeout
set ttimeoutlen=1
set ttyfast

And now it works pretty fine as you can see:

fix delay going back to block as cursor

I hope it could help any one else! 

Solution 5 - Vim

To change the shape of the cursor in different modes, you can add the following into your .vimrc file.

For the GNOME Terminal (version 2.26):

if has("autocmd")
  au InsertEnter * silent execute "!gconftool-2 --type string --set /apps/gnome-terminal/profiles/Default/cursor_shape ibeam"
  au InsertLeave * silent execute "!gconftool-2 --type string --set /apps/gnome-terminal/profiles/Default/cursor_shape block"
  au VimLeave * silent execute "!gconftool-2 --type string --set /apps/gnome-terminal/profiles/Default/cursor_shape ibeam"
endif

If you use more than one profile in GNOME Terminal, you might have to adapt this to your profiles.

For Konsole in KDE4:

let &t_SI = "\<Esc>]50;CursorShape=1\x7"
let &t_EI = "\<Esc>]50;CursorShape=0\x7"

This works with multiple tabs and windows.

See also: “Change cursor shape in different modes” on Vim Tips Wiki.

Solution 6 - Vim

I find it useful to only have the cursor blinking in Insert mode and keep it static in other modes.

set guicursor+=n-v-c:blinkon0

Solution 7 - Vim

You may try the Terminus Vim plugin:

> In insert mode, the cursor shape changes to a thin vertical bar. In replace mode, it changes to an underline. On returning to normal mode, it reverts to the standard "block" shape.

Solution 8 - Vim

If you are using a modern version of nvim and you wanted to achieve this, you can avoid some of these fancy workarounds listed above.

The below settings will switch from block cursor in normal mode, to underline cursor in replace to line cursor in insert.

# ~/.tmux.conf
set -g default-terminal "screen-256color"
set -ga terminal-overrides ",*256col*:Tc"
set -ga terminal-overrides '*:Ss=\E[%p1%d q:Se=\E[ q',w

" ~/.vimrc
" Sets cursor styles
" Block in normal, line in insert, underline in replace
set guicursor=n-v-c-sm:block,i-ci-ve:ver25-Cursor,r-cr-o:hor20

I managed to get this working with the following settings pulled from these two sources.

tui-cursor-shape

guicursor

Solution 9 - Vim

I don't think this adds much to the answers that other people have already provided but I wanted to somehow summarise things in one place and also have links to the relevant references.

This is what the relevant snippet from my .vimrc looks like:

    " Cursor appearance
    "
    " See also: [1]'ANSI Control Functions Summary', [2]DECSCUSR, [3]xterm+tmux
    "   entry in terminfo.src.
    " [1] https://www.vt100.net/docs/vt510-rm/chapter4.html
    " [2] https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
    " [3] https://raw.githubusercontent.com/mirror/ncurses/master/misc/terminfo.src
    "
    " On:
    " - entered insert mode
    let &t_SI = "^[[5 q^[]12;Magenta\007" " blinking bar (Ss) in magenta (Cs)
    " - entered replace mode
    let &t_SR = "^[[0 q^[]12;Red\007" " blinking block (Ss) in red (Cs)
    " - leaving insert/replace mode
    let &t_EI = "^[[2 q^[]112\007" " terminal power-on style (Se) and colour (Cr)

Note: The '^[' characters are actually one ESC (escape sequence) control character.

Solution 10 - Vim

According to this post on "Vim Tips WiKi":

"To change the shape of the cursor in different modes, you can add the following into your vimrc:"

"For Terminal on macOS"

"Mode Settings

let &t_SI.="\e[5 q" "SI = INSERT mode
let &t_SR.="\e[4 q" "SR = REPLACE mode
let &t_EI.="\e[1 q" "EI = NORMAL mode (ELSE)

"Cursor settings:

"  1 -> blinking block
"  2 -> solid block 
"  3 -> blinking underscore
"  4 -> solid underscore
"  5 -> blinking vertical bar
"  6 -> solid vertical bar

Scripts for other OS are also included in that post.

Solution 11 - Vim

This works properly on xfce4-terminal:

add following script to your .vimrc

if has("autocmd")
  au InsertEnter * silent execute "!sed -i.bak -e 's/TERMINAL_CURSOR_SHAPE_BLOCK/TERMINAL_CURSOR_SHAPE_IBEAM/' ~/.config/xfce4/terminal/terminalrc"                                                                                          
  au InsertLeave * silent execute "!sed -i.bak -e 's/TERMINAL_CURSOR_SHAPE_IBEAM/TERMINAL_CURSOR_SHAPE_BLOCK/' ~/.config/xfce4/terminal/terminalrc"                                                                                          
  au VimLeave * silent execute "!sed -i.bak -e 's/TERMINAL_CURSOR_SHAPE_IBEAM/TERMINAL_CURSOR_SHAPE_BLOCK/' ~/.config/xfce4/terminal/terminalrc"  
endif

Brief: As You know xfce4-terminal keeps preferences in .config/xfce4/terminal/terminalrc file. The script changes TERMINAL_CURSOR_SHAPE_BLOCK to TERMINAL_CURSOR_SHAPE_IBEAM when you're in insert mode, and back to block when you leave insert mode or vim. Feel free to change IBEAM to anything you want (BLOCK, IBEAM and UNDERLINE available).

Solution 12 - Vim

I usually have the current vim mode onto statusline, among other things. If you seek simplicity, you can set only this information onto the statusline.

However, usually the really crazy things happen when you have caps lock depressed and are in command mode (since hjkl now are HJKL - just J and K is enough to make you pull your hair out when you don't understand what's happening. Do a :h J and :h K to see what I mean). Just beware the caps lock key and you'll be fine most of the time IMO.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlanratView Question on Stackoverflow
Solution 1 - Vimib.View Answer on Stackoverflow
Solution 2 - VimlaktakView Answer on Stackoverflow
Solution 3 - Vimuser160917View Answer on Stackoverflow
Solution 4 - VimalexventuraioView Answer on Stackoverflow
Solution 5 - VimIvanGLView Answer on Stackoverflow
Solution 6 - Vimuser41365View Answer on Stackoverflow
Solution 7 - VimTim GabetsView Answer on Stackoverflow
Solution 8 - VimthrgamonView Answer on Stackoverflow
Solution 9 - VimZdzisław ŚliwińskiView Answer on Stackoverflow
Solution 10 - VimoatView Answer on Stackoverflow
Solution 11 - VimamiraliView Answer on Stackoverflow
Solution 12 - VimGmonCView Answer on Stackoverflow