How to make cursor change from thin line to block based on normal or insert mode in Console Vim on Gnome Terminal

12,124

Solution 1

There's no need to use autocommands or gconftool for this – Vim now supports it natively.

Insert the following lines into your vimrc:

let &t_SI = "\<esc>[5 q"  " blinking I-beam in insert mode
let &t_SR = "\<esc>[3 q"  " blinking underline in replace mode
let &t_EI = "\<esc>[ q"  " default cursor (usually blinking block) otherwise

These sequences should work in all VTE-based terminal emulators since VTE version 0.39, released at the end of 2014, as well as in xterm. If you'd like to stop the cursor blinking, add one to each of the numbers, and insert a 2 into the sequence for t_EI (the possible sequences are listed in this answer; see also the VT510 manual).

Solution 2

For gnome-terminal, add this to your ~/.vimrc (to be created if missing):

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

Found here: Change cursor shape in different modes.

EDIT

Change the last ibeam to block, to leave with a block cursor.

Solution 3

For gnome terminal version>3.15
Add this to your ~/.vimrc.

if has("autocmd")
  au VimEnter,InsertLeave * silent execute '!echo -ne "\e[2 q"' | redraw!
  au InsertEnter,InsertChange *
\ if v:insertmode == 'i' | 
\   silent execute '!echo -ne "\e[6 q"' | redraw! |
\ elseif v:insertmode == 'r' |
\   silent execute '!echo -ne "\e[4 q"' | redraw! |
\ endif
au VimLeave * silent execute '!echo -ne "\e[ q"' | redraw!
endif

You will get a block cursor in normal mode and a thin one in insert mode.

Share:
12,124
Jeromy Anglim
Author by

Jeromy Anglim

I am a Senior Lecturer in the School of Psychology at Deakin University bridging I/O psychology and statistics. I'm quite active on the Cognitive Sciences and Statistics Stack Exchanges. You can find me also on: Twitter: @JeromyAnglim My blog on psychology and statistics: http://jeromyanglim.blogspot.com

Updated on September 18, 2022

Comments

  • Jeromy Anglim
    Jeromy Anglim over 1 year

    I am switching from Gvim to using Console Vim using the Gnome Terminal 2.32.1.

    I really liked in Gvim how the cursor would be a solid square when in normal mode and thin line when in insert mode.

    • Is there a way of producing this functionality when running console Vim in the Gnome terminal?

    • If it's not possible, what tricks are there for knowing what mode you are in? I know there is the mode displayed at the bottom of the screen, but that doesn't seem to be as helpful as the cursor (which is where my eyes are looking). Or do you just get used to it with practice?

  • Jeromy Anglim
    Jeromy Anglim almost 13 years
    +1 Awesome. Thanks. I'm just seeing how well it plays with different use cases. I'm also getting a lag on conversion back to normal mode. Perhaps I have some mystery key binding to Escape and then something.
  • Jeromy Anglim
    Jeromy Anglim almost 13 years
    I guess the main issue as noted in the article is that this setting is system-wide, rather than being specific to one Vim session. Thus, any external consoles will be affected.
  • roeland
    roeland about 5 years
    This is now the best answer. Also found here: vim.fandom.com/wiki/Change_cursor_shape_in_different_modes
  • roeland
    roeland about 5 years
    gnome uses dconf now and this no longer works. There is now a better solution. (answer from Josh)