如何在 Gnome 终端中,在不同模式下更改 VIM 光标形状

11

我想根据当前模式更改VIM的光标(不是gVIM的)。我希望:

  • 正常和可视模式=块光标
  • 插入和命令模式= I形光标

我尝试将以下代码添加到.vimrc中,但它没有起作用。

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

我从http://vim.wikia.com/wiki/Change_cursor_shape_in_different_modes获取了那一小段代码,但它说这是针对Gnome-Terminal(版本2.26),而我使用的是Gnome-Terminal(版本3.60)。不确定这是否导致它无法工作。

有什么想法可以解决这个问题吗?


@eduan,你的代码是专门针对iTerm的。它在Gnome终端中无法运行。我找到的最好的解决办法就是更改光标的颜色。 - romainl
@romainl 哦,我明白了,我忘记了那个细节。 - greduan
你发布的配置对我有效,使用的是gnome-terminal 3.4.1.1版本。你是否使用“默认”gnome-terminal配置文件?如果不是,则需要将配置中的“默认”更改为您使用的配置文件名称。 - chreekat
3
您所拥有的东西是使用全局设置来解决本地问题,这会影响所有打开的终端窗口,而不仅仅是运行vim的那个窗口。 - Roger Lipscombe
2个回答

1

对我来说,gnidmoos的解决方案在将名为gnome-terminal-cursor-shape.sh的脚本更改为以下内容后起作用:

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

(使用.vimrc中的相同行)

附:我正在运行ubuntu 14.04,GNOME终端3.6.2。

干杯!


1

我有gnome-terminal 3.10.2,并通过以下步骤使其工作:

创建一个名为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'"

使用ibeam、block或underline调用它以更改光标形状。

将脚本放置在/usr/bin或/usr/local/bin中,并将以下行添加到您的.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

1
我可以确认在运行于xfce的gnome终端上可以工作。您需要在xfce应用程序自启动设置中启用“GSettings数据转换”。 - Rafael Vega
很遗憾,我无法应用这个技巧,因为它严格绑定在gnome-terminal上(而我正在使用Terminator)。不过我的评论是关于竞态条件的:这个解决方案是否会影响所有活动的gnome终端(即使没有运行vim的终端)? - Dacav

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接