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

帅比萌擦擦* 提交于 2019-12-09 03:29:19

问题


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?


回答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



回答2:


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!



来源:https://stackoverflow.com/questions/14266346/how-to-change-vim-cursor-shape-while-in-different-modes-in-gnome-terminal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!