Vim Tips Wiki

Change cursor shape in different modes

1,622pages on
this wiki
Tip 1622 Printable Monobook Previous Next

created March 28, 2009 · complexity basic · author Weltensegler · version 7.0

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

For the Gnome-Terminal (version 2.26)Edit

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"

For the Gnome-Terminal (version 3.x)Edit

First make a script

DEFAULTPROF=`dconf read /org/gnome/terminal/legacy/profiles:/default`
DEFAULTPROF=`echo "$DEFAULTPROF" | sed -e "s/^'/:/" -e "s/'$//"`
dconf write /org/gnome/terminal/legacy/profiles:/$DEFAULTPROF/cursor-shape "'$1'"

Make it executable & put it in /usr/local/bin Next, add this to .vimrc

if has("autocmd")
    au InsertEnter *
        \ if v:insertmode == 'i' |
        \   silent execute "! ibeam" |
        \ elseif v:insertmode == 'r' |
        \   silent execute "! underline" |
        \ endif
    au InsertLeave * silent execute "! block"
    au VimLeave * silent execute "! block"

If you use more than one profile in gnome-terminal, you might have to adapt this to your profiles.

For the Gnome-Terminal (version ≥3.16) Edit

This method has the advantage compared to the method above that it influences the given tab only, and not all the tabs that use the given profile. Add the following into .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!

For iTerm2 on OS X Edit

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

For tmux running in iTerm2 on OS X:

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

In theory, the escape sequence could be used for tmux in any terminal.

NOTE: These do not work with the default terminal.

For Konsole in KDE4Edit

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

This works with multiple tabs and windows.

For VTE compatible terminals (urxvt, st, xterm, gnome-terminal 3.x and others) Edit

let &t_SI = "\<Esc>[6 q"
let &t_SR = "\<Esc>[4 q"
let &t_EI = "\<Esc>[2 q"

Set IBeam shape in insert mode, underline shape in replace mode and block shape in normal mode.

For xfce-terminalEdit

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

See alsoEdit


In a vim_use thread on 31 March 2009, Matt Wozniski made the following comments:

Re the Gnome code: Unless I'm misunderstanding something, this would not only work improperly when combined with tools like screen and dvtm, but also, when you have multiple terminals open, it would change the cursor shape in all of them!

Re the KDE4 code: That won't play nicely with gnu screen, or with dvtm, or any of the other terminal multiplexers out there.

Both will probably have problems with multiple tabs.

Around Wikia's network

Random Wiki