需要一个脚本来模拟<cr>按插入模式

gvl*_*sov 2 vim

我正在编写一个在某些条件下自动插入一些文本的函数.当我尝试模仿Enter按时,我陷入了困境.我想出了这个(显而易见的)代码:

execute "normal! a\<cr>"
Run Code Online (Sandbox Code Playgroud)

问题是它打破了自动注册.例如,我有一个像这样的文本文件(|是游标):

if (condition) {
    action();|
Run Code Online (Sandbox Code Playgroud)

所以我跑了execute "normal! a\<cr>".当我需要这个时:

if (condition) {
    action();
    |
Run Code Online (Sandbox Code Playgroud)

它的行为如下:

if (condition) {
    action();
|
Run Code Online (Sandbox Code Playgroud)

当然,autoindent开启了.我究竟做错了什么?

And*_*Ray 5

它实际上按预期工作.如果你执行:execute "normal! a\<cr>hello它将把'hello'放在正确的位置.如果按AEnterEsc它,它将下降到该行的开头.该命令退出插入模式,因为它是一个空白行,不需要.

  • @Susei你可以插入一个字符,逃避,然后做`x`.例如,`:exe"norm!a\<cr> h\<esc> x"` (2认同)