The Objectively Correct Guide to Making Vim Use the System Clipboard

Make sure your vim has clipboard support (run vim --version and check for +clipboard), stick this in your .vimrc or neovim's init.vim, enjoy life:

set clipboard=
noremap ""y ""y
noremap ""yy ""yy
noremap ""p ""p
noremap ""P ""P
noremap  y  (v:register ==# '"' ? '"+' : '') . 'y'
noremap  yy (v:register ==# '"' ? '"+' : '') . 'yy'
noremap  p  (v:register ==# '"' ? '"+' : '') . 'p'
noremap  P  (v:register ==# '"' ? '"+' : '') . 'P'

That's it, post over, go home.

 

Wait, what?

Okay, so. Vi was originally invented in 1300 BC, before we had GUIs and even the idea of a shared system clipboard. What it had (and what Vim still has today) instead were registers. Registers are basically a bunch of internal Vim clipboards that you can use through the " key — for example, "fy to copy something into the f register, then "fp to paste from it.

By default, anything you yank (Vim's word for copy) goes into the unnamed register (which is actually assigned to the " key, so you can yank something into it explicitly by doing ""y. Then, what Vim's standard way of handling the system clipboard does is simply link up this unnamed register to your global clipboard. Nice and cool, until you overwrite everything you copied with a single character that you just deleted.

See, anything you delete using keys such as x, d, c, and the like, also goes into this unnamed register, overwriting it ("fd would delete something into the f register, by the way). This may have been okay if you were back in Heian Era Japan and all you had was a simple VT100 terminal to write love poems on, as you would have been living inside Vi anyway, using its registers extensively. However, in this day and age, you want to be consistent with the modern OS you're running Vim on (and if you aren't on a modern OS, the Vim on the ancient computer you're configuring is compiled without clipboard support anyway).

Anyone first starting to learn Vim will absolutely not be expecting their clipboard to be wiped every time they delete something, and it will drive them insane. They'll look around the internet for a solution, and they will only find two things.

The first is a bunch of ideology from Unix purists about how you're supposed to just live with that, get used to the Vim way, that poetry declined under the Kamakura shogunate, et cetera. Those people are important and help prevent this world from going completely off the rails drunk on overhyped technology, but listening to them for too long will quickly make the newcomer to Vim start to hate life. To any Vim newcomers reading this, now is also a good time to mention that if you want to have tabs in Vim, search for how to use Vim's buffers. Vim has another feature called tabs, but those are not actually tabs at all. It's complicated and everyone online will be telling you about the Vim way even though you just want to have a little bar on top with two text files that you can quickly switch between.

But anyway. Any newcomer not scared off at this point will keep looking and will find a bunch of solutions that remap the various delete commands to use the "void" (_) register. Those are okay, until you actually do want to use registers the Vim way, plus you'll have to play whack-a-mole with every single command that puts text in the clipboard register.

So what you really want is to just remap the yank (copy) and paste keys to use the system clipboard register. That's exactly what that snippet of code does.

The first line disables any sort of clipboard integration that may have been enabled by default (along with selections automatically getting yanked — change that to set clipboard=autoselect if you're really into that sort of thing and if your Vim supports it).

The last four lines change the main yank and paste keys: if you invoke a yank or paste command with the default unnamed register, it swaps that register out for the system clipboard one instead.

The four lines above those add an exception to the rule: if you explicitly try to use the unnamed register, it will be used instead. Make sure you get the order of the lines right in your Vim configuration file, or who knows what might happen.

So, with the clipboard set up this way, everybody wins! You get to have sane clipboard settings that work the same way as the rest of your OS, while still having the flexibility of proper registers if you need it.

The end!

P.S. If you find out that I made a mistake in my hubris, please email me at i@ezhik.me, or maybe make an issue on this site's GitHub if it's urgent enough to require public shaming. Thanks!