How to change VIM cursor shape while in different modes in Gnome Terminal
Asked Answered
F

2

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?

Fist answered 10/1, 2013 at 20:6 Comment(4)
@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.Detumescence
@Detumescence Ah I see, I forgot that detail.Toy
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.Alpine
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.Crumpet
K
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
Kilbride answered 17/6, 2014 at 12:50 Comment(2)
I can confirm this works on gnome-terminal running in xfce. You have to enable "GSettings data conversion" in xfce application autostart settings.Increment
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)?Basically
C
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!

Chaille answered 10/10, 2015 at 14:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.