使Vim中的MiddleMouse切换为插入(粘贴)、粘贴、然后转义

Make MiddleMouse in Vim switch to insert (paste), paste, and then escape

我正在使用通过 MobaXterm 连接到 CentOS 7 的 Windows 机器。我希望鼠标中键在 Vim 7.4 中工作(在没有剪贴板选项的情况下编译),就像在 linux 命令行中一样,即从我的 Windows 剪贴板中粘贴。

在我的 ~/.vimrc:

nnoremap <MiddleMouse> :set paste<cr>:startinsert<MiddleMouse><esc>

起初,我担心这可能不会达到我的预期,因为可能我不明白 Vim 是如何递归的,所以我用 Shift 尝试了一下+Insert,因为这是 MiddleMouse 为了粘贴而映射到的内容:

nnoremap <MiddleMouse> :set paste<cr>:startinsert<S-Insert><esc>

这给出了关于 E488: Trailing characters 的错误。

Edit:正如 Christian 所指出的,我在 :startinsert 命令后遗漏了一个 <cr>,但这仍然没有解决我的问题。

nnoremap <MiddleMouse> :set paste<cr>:startinsert<cr><S-Insert><esc>

不会再报错了,只是我粘贴前没有进入insert模式

这显然是预期的行为(呃)。

:star[tinsert][!]   Start Insert mode just after executing this command.
                    Works like typing "i" in Normal mode.  When the ! is
                    included it works like "A", append to the line.
                    Otherwise insertion starts at the cursor position.
                    Note that when using this command in a function or
                    script, the insertion only starts after the function
                    or script is finished.
                    This command does not work from :normal.

这意味着我在 :startinsert 之后放置的所有命令,而不是 运行 紧接在 :startinsert 之前,然后是 :startinsert 运行 和更改插入模式(注意:这似乎也适用于使用 i 而不是 :startinsert)。

我的下一步是尝试创建一个嵌套函数,其中一个函数调用另一个函数,第二个函数 运行s :startinsert 然后 returns 到第一个函数,然后完成粘贴:

function! Paste()
  call InsertMode()<cr>
  :set paste<cr>
  <S-Insert>
  :set nopaste<cr>
  <esc>
endfunction
function! InsertMode()
  :startinsert<cr>
endfunction
nnoremap <MiddleMouse> call Paste()<cr>

但这也不起作用。我也尝试使用 "+p"*p 寄存器而不使用 :startinsert 正如 Christian 在他对 nnoremap <MiddleMouse> :set paste<cr>"+p:set nopaste<cr> 的评论中所说的那样,但这再次直接粘贴,就像我输入它一样,它不会首先进入 insert 模式。我愿意相信这适用于使用 +clipboard 编译的 Vim 版本,但那不是我拥有的版本。

我能做的最好的事情是在进入 insert 模式后使用 MiddleMouse 按钮​​正确粘贴:

inoremap <MiddleMouse> :set paste<cr><S-Insert>:set nopaste<cr>

如果有人知道在 normal 模式下执行此操作的方法,请告诉我。看起来应该是可能的,但我读过的所有内容都表明没有 +clipboard 选项(可使用 vim --version | grep clipboard 检查)它不是。