Search code examples
copy-pasteneovim

How can I paste NeoVim registers in terminal mode?


When using NeoVim's :terminal feature, I'd like to be able to paste text from a register.

When working with a standard text buffer, I frequently use two methods to paste text from a register:

  • in Normal mode: "{register}p
  • in Insert mode: <C-r>{register}

I can't get these commands to work the way I'd like them to in a terminal buffer. When the terminal buffer is in Normal mode, I can use "{register}p to append the contents of a register at the end of the current command line. Sometimes I'd like to insert text at the start of the current command line, or midway through it, and it seems like there's no way of doing that using this command.

It occurs to me that this should be possible by switching to Terminal mode (which seems to be the terminal buffer equivalent to Insert mode) and using the <C-r>{register} command. But the ctrl+r is sent directly to my shell. I'm using the bash shell, which maps these keys to the reverse search feature, so I find myself looking at this prompt:

(reverse-i-search)`':

Is there any way that I can use the <C-r>{register} in Terminal mode?


Solution

  • Nvim is conservative about handling any keys (except <C-\><C-N>) in terminal-mode, so CTRL-R isn't handled by default. Also, Nvim has no idea about which application is running inside :terminal, so p just blindly sends text to the input stream.

    Having said that, you can use :tnoremap to get a similar effect for CTRL-R:

    :tnoremap <expr> <C-R> '<C-\><C-N>"'.nr2char(getchar()).'pi'