Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 980
lanrat Avatar asked Jun 27 '11 04:06

lanrat


4 Answers

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

like image 175
ib. Avatar answered Nov 02 '22 18:11

ib.


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'
like image 22
laktak Avatar answered Nov 02 '22 16:11

laktak


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! 👻

like image 20
alexventuraio Avatar answered Nov 02 '22 17:11

alexventuraio


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

like image 17
user160917 Avatar answered Nov 02 '22 17:11

user160917