Vim CursorLine color change in insert mode

This is pretty straightforward, put the following in your .vimrc or custom colorscheme file.

set cursorline
autocmd InsertEnter * highlight CursorLine guifg=white guibg=blue ctermfg=white ctermbg=blue
autocmd InsertLeave * highlight CursorLine guifg=white guibg=darkblue ctermfg=white ctermbg=darkblue

For more information see:

  • :help 'cursorline'
  • :help :autocmd
  • :help InsertEnter
  • :help :highlight

N.B: You can use the same method to change the colour of the cursor without all of those if-statements and escape-sequences (and this will also work in GVim).


Have you look in into the 'highlight' command which is a easier way to control this.

For example, to change the CursorLine,

:hi CursorLine guifg=red guibg=blue

Reference: :help highlight

To make it switch between mode.

" Enable CursorLine
set cursorline

" Default Colors for CursorLine
highlight  CursorLine ctermbg=Yellow ctermfg=None

" Change Color when entering Insert Mode
autocmd InsertEnter * highlight  CursorLine ctermbg=Green ctermfg=Red

" Revert Color to default when leaving Insert Mode
autocmd InsertLeave * highlight  CursorLine ctermbg=Yellow ctermfg=None

I may be possible to mix termcap color with autocmd, but IMO, highlight is more easy to maintain in long term (and in case if use gVim occassionally)