命令模式下的垂直 vim 光标

Vertical vim cursor in command mode

我在 mac 上,我将终端光标设置为竖线选项。但是在 vim 命令模式下,光标是垂直条,但它不会让我使用 hjkl 转到行尾,它总是在行尾之前停止。这特别烦人,因为您必须在插入模式下使用箭头键才能使光标移动到行尾。任何修复将不胜感激

例如:你好世界 | d , 我要的是 hello world |

我想你正在寻找 set virtualedit=onemore

来自:help 'virtualedit'

A comma separated list of these words:
    block   Allow virtual editing in Visual block mode.
    insert  Allow virtual editing in Insert mode.
    all     Allow virtual editing in all modes.
    onemore Allow the cursor to move just past the end of the line

[...]

"onemore" is not the same, it will only allow moving the cursor just
after the last character of the line.  This makes some commands more
consistent.  Previously the cursor was always past the end of the line
if the line was empty.  But it is far from Vi compatible.  It may also
break some plugins or Vim scripts.  For example because l can move
the cursor after the last character.  Use with care!

我自己从来没有注意到任何问题,因此尽管有警告,它似乎还是相当安全的。

如果您使用的是 iTerm2,有一个小技巧:您可以根据您的模式自动切换光标。如果您的默认光标是块光标,并且您只需要插入模式下的垂直条,那么效果会更好,但我还是会显示它:

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

这指示 vim 在进入 (&t_SI) 和退出 (&t_EI) 插入模式时打印这些字符串。 iTerm2 有一堆 proprietary escape codes 将字符串解释为更改光标形状的指令。

然后你需要做的就是在启动 vim 时以某种方式打印 "\<Esc>]50;CursorShape=0\x7" 并在退出时打印 "\<Esc>]50;CursorShape=1\x7" 。为此,您可以使用 autocmds:

autocmd BufEnter * execute 'silent !echo -ne "' . &t_EI . '"'
autocmd VimLeave * execute '!echo -ne "' . &t_SI . '"'

这会在输入时自动将光标形状更改为框 vim,然后在退出时将其恢复为垂直条。