11

I would like to change VIM's (not gVIM's) cursor depending on what mode I am currently in. I would like:

  • Normal & Visual modes = block cursor
  • Insert & Command modes = I beam cursor

I tried adding the following code to .vimrc but it did not work.

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

I got that bit of code from http://vim.wikia.com/wiki/Change_cursor_shape_in_different_modes but it says that it is for Gnome-Terminal (version 2.26) and I have Gnome-Terminal (version 3.60). Not sure if that is the reason why it's not working.

Any ideas on how to do this?

  • @eduan, your code is iTerm-specific. It doesn't work in Gnome terminal. The best I could find is changing the color of the cursor. – romainl Jan 10 '13 at 20:39
  • @romainl Ah I see, I forgot that detail. – greduan Jan 10 '13 at 20:41
  • The configuration you posted works for me, with gnome-terminal version 3.4.1.1. Are you using the "Default" gnome-terminal profile? If not, you'd have to change "Default" in the configuration to the name of the profile you use. – chreekat Jul 22 '13 at 11:08
  • 3
    Also, what you have there is using a global setting to solve a local problem -- this affects all open terminal windows, not just the one running vim. – Roger Lipscombe Oct 31 '13 at 14:03

2 Answers2

1

I have gnome-terminal 3.10.2 and I got it working with the following steps:

Create a script called gnome-terminal-cursor-shape.sh:

#!/bin/sh
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'"

And call it with ibeam, block or underline to change cursor shape.

Put the script in /usr/bin or /usr/local/bin, and add the following lines to your .vimrc:

if has("autocmd")
    au InsertEnter *
        \ if v:insertmode == 'i' |
        \   silent execute "!gnome-terminal-cursor-shape.sh ibeam" |
        \ elseif v:insertmode == 'r' |
        \   silent execute "!gnome-terminal-cursor-shape.sh underline" |
        \ endif
    au InsertLeave * silent execute "!gnome-terminal-cursor-shape.sh block"
    au VimLeave * silent execute "!gnome-terminal-cursor-shape.sh block"
endif
gnidmoo
  • 465
  • 3
  • 13
  • 1
    I can confirm this works on gnome-terminal running in xfce. You have to enable "GSettings data conversion" in xfce application autostart settings. – Rafael Vega Sep 10 '14 at 17:40
  • Unfortunately I can't apply this trick, because it's strictly bound to gnome-terminal (while I'm using Terminator). My comment however is about race conditions: could it be that this solution affects all alive gnome-terminals (even the ones not running vim)? – Dacav Nov 06 '14 at 08:43
1

For me, gnidmoos solution worked after changing the script script called gnome-terminal-cursor-shape.sh to:

#!/bin/sh
gconftool-2 --set "/apps/gnome-terminal/profiles/Default/cursor_shape" --type string "$1"

(using the same lines in .vimrc)

Ps. I'm running ubuntu 14.04, GNOME Terminal 3.6.2

Cheers!