如何在 Vim 中更改“正常”和“插入”模式之间的光标?

我想知道如何改变,如果可能的话,光标在 Vim (颜色,形状等)取决于你在什么模式。

我经常忘记,我不是在插入模式,并开始键入代码,这导致各种疯狂的事情发生。如果光标上有某种形式的视觉指示将会很有帮助。

96970 次浏览

若要以不同模式更改光标的形状,可以将以下内容添加到 .vimrc文件中。

对于 GNOME 终端(版本2.26) :

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

如果您在 GNOME 终端中使用了多个配置文件,那么您可能必须将其调整到您的配置文件中。

KDE4中的 Konsole:

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

这适用于多个选项卡和窗口。

另见: Vim Tips Wiki上的“ 在不同模式下更改光标形状”。

指示切换到 Insert 模式和切换到 Insert 模式的一种流行方法是 切换 cursorline选项,该选项负责 突出显示当前屏幕线(见 :help cursorline) :

:autocmd InsertEnter,InsertLeave * set cul!

或者,也可以说:

:autocmd InsertEnter * set cul
:autocmd InsertLeave * set nocul

修改 CursorLine高亮显示组以更改样式 的光标行,以您的喜好(见 :help :highlight:help highlight-groups).

我通常将当前的 vim 模式设置为 statusline 等等。

然而,通常真正疯狂的事情发生时,你有大写锁定抑制和在命令模式(因为 HJKL 现在是 HJKL-只是 J 和 K 就足以让你拉你的头发时,你不明白发生了什么。做一个 :h J:h K看看我的意思)。只要 小心盖子上的锁匙和你会罚款的大部分时间,我们。

我发现只让光标在插入模式下闪烁并在其他模式下保持静态非常有用。

set guicursor+=n-v-c:blinkon0

如果你在 MacOS上使用 TMuxITerm2,
下面将游标从块更改为游标并突出显示当前行

if exists('$TMUX')
let &t_SI = "\<Esc>Ptmux;\<Esc>\<Esc>]50;CursorShape=1\x7\<Esc>\\"
let &t_EI = "\<Esc>Ptmux;\<Esc>\<Esc>]50;CursorShape=0\x7\<Esc>\\"
else
let &t_SI = "\<Esc>]50;CursorShape=1\x7"
let &t_EI = "\<Esc>]50;CursorShape=0\x7"
endif
:autocmd InsertEnter * set cul
:autocmd InsertLeave * set nocul

来源: https://gist.github.com/andyfowler/1195581

以下内容适用于 Linux 上的 xterm、 urxvt 和其他终端模拟器; macOS 上的 iTerm2; Windows 上的 Git Bash 和 ConEmu; 以及更多内容(见注释) :

let &t_SI = "\e[6 q"
let &t_EI = "\e[2 q"


" reset the cursor on start (for older versions of vim, usually not required)
augroup myCmds
au!
autocmd VimEnter * silent !echo -ne "\e[2 q"
augroup END

其他选项(替换 \e[后面的数字) :

Ps = 0  -> blinking block.
Ps = 1  -> blinking block (default).
Ps = 2  -> steady block.
Ps = 3  -> blinking underline.
Ps = 4  -> steady underline.
Ps = 5  -> blinking bar (xterm).
Ps = 6  -> steady bar (xterm).

当您使用 TMux时,像这样使用它(不使用 \<Esc>Ptmux;转义)是很重要的。Tmux 将在切换窗口/窗格时跟踪正确的光标形状。

如果它对你不起作用,尝试在开始 tmux 之前设置 TERM=xterm-256color,或者将它添加到你的 .tmux.conf(谢谢@Steven Lu) :

set -ga terminal-overrides ',*:Ss=\E[%p1%d q:Se=\E[2 q'

你可以试试 终端 Vim 插件:

在插入模式下,光标形状更改为一个细垂直条。在替换模式下,它更改为下划线。在返回到正常模式时,它恢复到标准的“块”形状。

这在 xfce4终端上可以正常工作:

将以下脚本添加到 .vimrc

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

简介: 如您所知,xfce4-终端在 .config/xfce4/terminal/terminalrc文件中保存首选项。当您处于插入模式时,该脚本将 TERMINAL_CURSOR_SHAPE_BLOCK更改为 TERMINAL_CURSOR_SHAPE_IBEAM,当您离开插入模式或 vim 时,该脚本将返回到块。随意改变 IBEAM到任何你想要的(BLOCKIBEAMUNDERLINE可用)。

不知道是否有其他人面临延迟后,按下 Esc键回到正常模式显示块光标,但如果是这样,这是我修复它的方式太。

实际上,我正在 MacOS 上使用 iTerm2和终端中的 Vim。当进入插入模式时,光标仍然是一个块,当您处于插入模式或正常模式时会有点混乱。

我希望在插入模式下显示一条细线作为光标,在正常模式下显示返回块,就像 MacVim 那样。要做到这一点非常简单,只需要把它作为 这里描述的添加到我的 .vimrc文件中:

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

enter image description here

但是正如您所看到的,当按下 ESC退出插入模式回到正常模式并再次将块显示为光标时有一个延迟。为了解决这个问题,我发现了这个:

set ttimeout
set ttimeoutlen=1
set ttyfast

如你所见,现在它运行得非常好:

fix delay going back to block as cursor

我希望它能帮助任何人!

如果您使用的是 nvim的现代版本,并且希望实现这一点,那么可以避免上面列出的一些奇特的变通方法。

下面的设置将从正常模式下的块光标切换到插入中的下划线光标替换为行光标。

# ~/.tmux.conf
set -g default-terminal "screen-256color"
set -ga terminal-overrides ",*256col*:Tc"
set -ga terminal-overrides '*:Ss=\E[%p1%d q:Se=\E[ q',w


" ~/.vimrc
" Sets cursor styles
" Block in normal, line in insert, underline in replace
set guicursor=n-v-c-sm:block,i-ci-ve:ver25-Cursor,r-cr-o:hor20

我设法让这个工作与下列设置从这两个来源拉。

推-光标-形状

光标

我不认为这增加了很多其他人已经提供的答案,但我想以某种方式总结在一个地方的东西,也有相关参考的链接。

下面是我的 .vimrc中的相关片段:

    " Cursor appearance
"
" See also: [1]'ANSI Control Functions Summary', [2]DECSCUSR, [3]xterm+tmux
"   entry in terminfo.src.
" [1] https://www.vt100.net/docs/vt510-rm/chapter4.html
" [2] https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
" [3] https://raw.githubusercontent.com/mirror/ncurses/master/misc/terminfo.src
"
" On:
" - entered insert mode
let &t_SI = "^[[5 q^[]12;Magenta\007" " blinking bar (Ss) in magenta (Cs)
" - entered replace mode
let &t_SR = "^[[0 q^[]12;Red\007" " blinking block (Ss) in red (Cs)
" - leaving insert/replace mode
let &t_EI = "^[[2 q^[]112\007" " terminal power-on style (Se) and colour (Cr)

注意: '^['字符实际上是一个 ESC(转义序列)控制字符。

根据 Vim Tips WiKi台的 邮寄节目:

“要在不同模式下更改光标的形状,可以在 vimrc 中添加以下内容:”

“ For Terminal on macOS”

"Mode Settings


let &t_SI.="\e[5 q" "SI = INSERT mode
let &t_SR.="\e[4 q" "SR = REPLACE mode
let &t_EI.="\e[1 q" "EI = NORMAL mode (ELSE)


"Cursor settings:


"  1 -> blinking block
"  2 -> solid block
"  3 -> blinking underscore
"  4 -> solid underscore
"  5 -> blinking vertical bar
"  6 -> solid vertical bar

其他操作系统的脚本也包含在这篇文章中。