Search code examples
vimneovim

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


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("hey");
}

and then paste it in insert mode with i<c-r>" it will be messed up:

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

Whereas pasting the same snippet from the system clipboard (i.e. with Ctrl+Shift+v) 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?


Solution

  • 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