Copy & paste between Vim and your system clipboard

Yanking (copying) text with y in Vim and then trying to paste it elsewhere won’t work. Likewise, copying text outside Vim and pasting it into Vim with p won’t work either. The reason is that by default, Vim stores yanked text in its internal unnamed register ", which is separate from the system clipboard.

To interact with the system clipboard, we have to explicitly use the + register. For example, we can use "+yy to yank a line into the system clipboard, "+y to yank a visual selection into the clipboard, and "+p to paste from the clipboard into Vim. For this to work though, Vim must be compiled with clipboard support.1

However, typing "+ every time is quite inconvenient, so it’s a good idea to set up some mappings in ~/.vimrc, like the following:

nnoremap gy "+y
vnoremap gy "+y
nnoremap gp "+p
nnoremap gP "+P
vnoremap gp "+p

This keeps the normal y and p working with Vim’s own registers, and gives us the gy and gp whenever we want to use the system clipboard.


  1. You can check it by running :version inside Vim and looking for +clipboard. If you see -clipboard, your Vim doesn’t support it and you need to install a version that does, like vim-gtk3↩︎