Learn Vim at your own pace with my self-study Core Vim Course.

Learn more

Learn Vim at your own pace with my self-study Core Vim Course.

Pasting into a terminal buffer


Run time:

Yanking and pasting works seemlessly between Neovim’s regular buffers and terminal buffers. In this video, we’ll look at how the Normal mode paste command works in a terminal buffer, and we’ll create a mapping to help with pasting text directly from Terminal mode.


Suppose that you’ve opened a regular text buffer containing a README.md file, which lists a series of commands that you want to run in a shell. In another split, you’ve got a terminal buffer containing a shell. You can activate the text buffer and use the "ay command to yank the text. Then activate the terminal buffer and use the Normal mode "ap command to insert the text into the shell’s command line. To execute that command line in the shell, you could then press i<Enter>. (i switches to Terminal mode, then the shell receives the <Enter> key which executes the command line.)

How does a terminal buffer handle pasted text?

If you followed the example above, you could come away with the impression that there’s nothing special about using the p command in a terminal buffer. But you should be aware of some of the differences in how the p command works in a terminal buffer, compared with a regular buffer.

In a regular buffer, the p command pastes text at the cursor position. (To split hairs: lowercase p pastes after the cursor, whereas uppercase P pastes before the cursor.) In a terminal buffer, the p command pastes text at the location of the terminal cursor, which is distinct from the normal cursor. So what’s the difference?

Suppose that you have a bash shell running inside of a terminal buffer. In this scenario Neovim has a cursor and Bash has a cursor. When you’re in Normal mode, you can use Neovim motions such as j, k, w, b to move Neovim’s cursor. When you’re in Terminal mode, you can use readline bindings such as <C-a>, <C-e>, <A-f>, <A-b> to move Bash’s cursor. When you use the Normal mode p command, Neovim sends the text from the register to the underlying program, just as if you had typed the characters by hand in Terminal mode. In this context, the outcome is that the text from the register gets inserted into the bash shell at the location of the bash cursor.

Making the terminal cursor visible

You can style the appearance of the terminal cursor using the TermCursor and TermCursorNC highlight groups. The TermCursor style applies when you’re in Terminal mode, otherwise the TermCursorNC style is used (NC stands for Non-Current). If your colorscheme doesn’t specify styles for these highlight groups, then it can be hard to tell where the text is going to appear when we use the p command.

You can make your terminal cursor more visible using this quick fix:

:hi! TermCursorNC ctermfg=15 guifg=#fdf6e3 ctermbg=14 guibg=#93a1a1 cterm=NONE gui=NONE

A more long-term solution would be to add suitable styles to your preferred colorscheme. I’m currently enjoying using the solarized8 theme, which defines styles for Neovim’s terminal.

Pasting in terminal mode

In Insert mode, you can use <C-r>{reg} to insert the contents of a register. However, this command does not work in Terminal mode. Remember: in Terminal mode, all keystrokes are forwarded to the program that’s running inside of the terminal buffer. For example, if you’re running a bash shell (with readline default bindings) inside a terminal buffer, then pressing <C-r> will invoke the reverse incremental search feature. It would be handy if you could paste text straight from Terminal mode.

You can create a mapping to add this functionality:

if has('nvim')
  tnoremap <expr> <A-r> '<C-\><C-N>"'.nr2char(getchar()).'pi'

After loading this mapping, you can insert the contents of a register in Terminal mode by pressing <A-r>{reg}. If you’d prefer to make this mapping resemble the Insert mode mapping, you could instead bind it to <C-r>, but that would mean that you couldn’t send <C-r> to the program running inside the terminal buffer.

Further reading


Level-up your Vim


Boost your productivity with a Vim training class. Join a public class, or book a private session for your team.

Drew hosted a private Vim session for the shopify team that was one of the best workshops I have ever attended.

John Duff, Director of Engineering at Shopify


Make yourself a faster and more efficient developer with the help of these publications, including Practical Vim (Pragmatic Bookshelf 2012), which has over 50 five-star reviews on Amazon.

After reading it, I've switched to vim as my default editor on a daily basis with no regrets. ★★★★★

Javier Collado

Learn to use Vim efficiently in your Ruby projects

In association with thoughtbot, one of the most well respected Rails consultancies in the world, I've produced a series of screencasts on how to make navigating your Ruby projects with Vim ultra-efficient. Along the way, you’ll also learn how to make Ruby blocks a first-class text object in Vim. This lets you edit Ruby code at a higher level of abstraction. Available to buy from thoughtbot..