如何在Vim/Neovim的插入模式下使用 bracketed-paste-mode 粘贴寄存器?

huangapple go评论50阅读模式
英文:

How to use bracketed-paste-mode in Vim/Neovim when pasting a register in insert mode?

问题

在使用Vim/Neovim的插入模式中,从寄存器中插入代码时,不会自动使用xterm-bracketed-paste(Vim) / bracketed-paste-mode功能。

例如,如果我已经在demo.js文件中复制了这个函数:

function x() {
  // comment
  alert("hey");
}

然后在插入模式下使用i<C-r>"粘贴它,它会出现混乱:

function x() {
    // comment
  //   alert("hey");
  //   }
//

而从系统剪贴板中插入相同的代码段(即使用Ctrl+Shift+v)在插入模式下可以正常工作,这是由于支持方括号粘贴功能。

我知道我可以在粘贴之前使用:set paste。但这样的话,要么我必须离开插入模式,要么 - 当映射到一个键时 - 按下一个键来启用粘贴模式。

是否有一种自动化这个过程的方法?

英文:

When inserting code from a register in insert mode with Vim/Neovim it will not automatically use xterm-bracketed-paste(Vim) / bracketed-paste-mode(Neovim).

For example if I have yanked this function in a demo.js file:

function x() {
  // comment
  alert(&quot;hey&quot;);
}

and then paste it in insert mode with i&lt;c-r&gt;&quot; it will be messed up:

function x() {
    // comment
  //   alert(&quot;hey&quot;);
  //   }
//

Whereas pasting the same snippet from the system clipboard (i.e. with <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>v</kbd>) in insert mode works fine due to the bracketed paste feature.

I know that I can use :set paste before pasting. But then I either have to leave insert mode or - when mapped to a key - press a key to enable paste mode.

Is there a way to automate this?

答案1

得分: 1

正如其他人已经提到的,i_CTRL-R 会插入文本,就好像是手动输入的一样。但是,还有其他一些相关的键绑定可以实现你想要的效果:

i_CTRL-R_CTRL-O 会插入文本,而不会自动缩进和应用其他格式选项。然而,如果你要粘贴代码,通常需要调整缩进到当前的级别。i_CTRL-R_CTRL-P 会在粘贴文本时自动重新缩进。

更多详细信息可以在 Vim 帮助页面中找到,使用命令 :help i_ctrl-r

英文:

As others already said, i_CTRL-R inserts text as if typed. However, there are various other related keybinds that accomplish what you want:

i_CTRL-R_CTRL-O inserts text without auto-indentation and other format options. However, if you're pasting code, you will often want to adjust the indentation to the current level. i_CTRL-R_CTRL-P will automatically re-indent text as it is pasted in.

More details can be found in Vim help pages at :help i_ctrl-r

答案2

得分: 0

:h &lt;c-r&gt; 指出了以下内容:

> 文本插入的方式就像你手动输入一样,但不会使用映射和缩写。

因此,它不会触发任何粘贴命令或回调或任何其他操作。它只是一种自动输入文本的方式。

对于这种问题的答案始终是相同的:离开插入模式。
Vim 是基于模式的,这是 Vim 强大之处的基础。插入模式不是专门用于粘贴内容(尽管我喜欢 &lt;c-r&gt;)。在插入模式下,有很多事情是做不了的。

英文:

:h &lt;c-r&gt; does state the following:

> The text is inserted as if you typed it, but mappings and abbreviations
> are not used.

So, it does not trigger any paste commands or callbacks or anything. It is just an automated way to type in text.

The answer to problems like this is always the same: Leave insert mode.
Vim is mode based, that is the fundament of the power of vim. Insert mode is not specialized to paste stuff (although I love &lt;c-r&gt;). There is a lot of stuff you can't to in insert mode.

答案3

得分: 0

我尝试了以下映射,在插入模式下粘贴系统剪贴板。到目前为止,它似乎能够满足我的需求。

inoremap <c-p> <c-o>:set paste<cr><c-r>+<c-o>:set nopaste<cr>
英文:

I've tried the following mapping to paste the system clipboard in insert mode. So far it seems to do what I want.

inoremap &lt;c-p&gt; &lt;c-o&gt;:set paste&lt;cr&gt;&lt;c-r&gt;+&lt;c-o&gt;:set nopaste&lt;cr&gt;

huangapple
  • 本文由 发表于 2023年2月16日 17:34:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75470297.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定