158

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.

3
  • 29
    I suggest you to learn one rule: stopped typing => exit insert mode. Then you will never observe a situation where you forgot that you are not in insert mode.
    – ZyX
    Jun 28, 2011 at 15:29
  • 6
    @ZyX except when you forget to exit the insert mode after having stopped typing :p
    – gented
    May 9, 2020 at 10:10
  • 13
    @ZyX that's a fine rule and I have always done that, but visual indicators are still important. Marijuana is legal now and I forget what I'm doing all the time.
    – felwithe
    Jun 17, 2020 at 13:42

12 Answers 12

154

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'
16
  • 4
    Works great in alacritty now
    – Steven Lu
    Dec 12, 2017 at 22:16
  • Works perfect in xfce4-terminal
    – korst1k
    Dec 25, 2017 at 16:04
  • @Charles About why it does not work in putty, see this: superuser.com/questions/304318/…
    – WesternGun
    Apr 26, 2018 at 16:14
  • 1
    My Update: working finally with set -ga terminal-overrides ',*:Ss=\E[%p1%d q:Se=\E[2 q' in my tmux configuration. Apparently if you use set for terminal-overrides without -a, it will override the setting, which already has this there by default.
    – Steven Lu
    Sep 30, 2019 at 17:09
  • 2
    the solution has a significant delay of changing shape from insert mode to normal mode on simple terminal (suckless st)
    – daGo
    Mar 23, 2020 at 20:05
135

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 * set cursorline
:autocmd InsertLeave * set nocursorline

or, alternatively:

:autocmd InsertEnter,InsertLeave * set cul!

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

This approach assumes, of course, that you do not use the cursor line highlighting in Normal mode.

4
  • 9
    I also set cul to higlight bg instead of default underline: hi CursorLine cterm=NONE ctermbg=black Nov 10, 2011 at 8:21
  • 1
    Is there a reason this wouldn't work in cygwin's vim? I'm in the editor and entered both set of lines, and I haven't been able to set any sort of highlighting, beyond the full column or row highligting Jul 9, 2012 at 17:26
  • 1
    @Tankor: The cursorline option does not depend on any platform-specific capability; it is available on any Vim instance compiled with the +syntax feature (which is usually the case).
    – ib.
    Jan 15, 2014 at 5:52
  • 1
    Of course, save that to your ~/.vimrc for permanence like zo: echo 'autocmd InsertEnter,InsertLeave * set cul!' >>~/.vimrc
    – alchemy
    Jun 1, 2020 at 0:17
31

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

2
  • 2
    The mode change lag was bugging me for so long. Thank you!
    – outerSpace
    Dec 17, 2021 at 15:46
  • 2
    @leedreit same for me until I found this workaround! Dec 18, 2021 at 16:36
20

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

4
  • 1
    Note this is specific to iTerm. Since the standard ones actually also work but also work in all the other supporting terminals, this is probably the wrong answer. Look here --> stackoverflow.com/a/42118416/340947
    – Steven Lu
    Dec 12, 2017 at 22:06
  • @StevenLu actually the one in your suggested answer didn't work for iTerm2 (Mac), while this one worked. This stuff is more fiddly than you might expect.
    – C S
    Sep 29, 2019 at 7:57
  • This cursor stuff is definitely one of the fiddlier things out there. I just realized that don't even think I have a proper setup, as tmux does not appear to be performing cursor state tracking which some answers indicate is a possibility. I was plenty happy enough just to get vim to handle setting the cursor to a line on insert mode.
    – Steven Lu
    Sep 29, 2019 at 12:40
  • I figured this stuff out. The solution is to tweak tmux to properly handle it, and it can be non-obvious. What I needed to do was ensure that my tmux config's terminal-overrides was only being appended to, so it didn't clobber the ,*:Ss=\E[%p1%d q:Se=\E[2 q part of terminal-overrides. so my config looks like set -sa terminal-overrides ",xterm-256color-italic:Tc" (to set the 24-bit color enabling without trampling cursor styles Ss/Se.
    – Steven Lu
    Oct 4, 2019 at 0:51
13

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.

5
  • 14
    I primarily use vim in tmux over ssh with putty. Is there a more universal option?
    – lanrat
    Jun 27, 2011 at 5:21
  • 1
    Changing gnome-terminal's settings via a gconftool-2 command is a very bad idea. It does not only influence the terminal where your vim is running, but also all other current and future gnome-terminal tabs of the same profile.
    – egmont
    Sep 11, 2016 at 18:38
  • 1
    this works for konsole, thank you. But one issue is that exiting insert mode is slooow, not sure why
    – Hoang Tran
    Jul 2, 2017 at 11:35
  • 1
    @HoangTran use set ttimeoutlen=0
    – Bananach
    Aug 30, 2017 at 7:10
  • how to apply this to all profiles. Eg I have One light and Foxnight profiles - When I use Default it only works for Foxnight profile.
    – daGo
    Oct 24, 2019 at 11:54
11

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.

2
  • This seems like the most sensible solution. There should be a :set for this directly in VIM, standard.
    – user12711
    Aug 9, 2019 at 1:11
  • Requirements: gnome-terminal (wiki.gnome.org/Apps/Terminal) and other terminals using the VTE library (github.com/GNOME/vte): Partially supported (for example, supports cursor shape changes but not focus reporting). In my case (gnome-terminal 3.6.2) it doesn't work at all.
    – daGo
    Nov 17, 2019 at 6:30
10

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
1
  • 1
    This solution causes a cursor to blink in all modes. Apr 10, 2021 at 5:42
8

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.

1

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

1

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.

0

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

-1

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.

2
  • 46
    How does this answer the question? Aug 4, 2015 at 19:55
  • 2
    @DavidRivers It's an application of the XY problem where the OP was asking for a resolution to his problem (not knowing about being in vim normal mode and wanting the cursor to change to indicate it) but turned out satisfied with an alternative (and IMO not a great) solution (enhancing his statusline with the state)
    – Steven Lu
    Dec 12, 2017 at 22:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.