Vim: Yank Into Clipboard
I use the Vim editor on Linux. If you use Linux you probably know that it usually has two text buffers that behave as clipboards: the primary selection, which is populated immediately upon selecting text without any further action by the user, and the clipboard, which is populated with an explicit copy command, using either a mouse menu or a shortcut, usually ctr-c
.
To get text out of the primary selection, you need to click the middle mouse button (or do some other thing with buttons if you don’t have a middle one). To paste from the clipboard, you must use a mouse menu or a shortcut, usually ctr-v
.
Sometimes I want to get a snippet of text from a Vim buffer, such as a URL, and paste it in somewhere, say a web browser or terminal. The version of Vim I use automatically puts text that I have highlighted with the visual selection (v
or V
) into the primary selection, which is a convenience of sorts. Unfortunately to paste the selection after switching away from Vim I am forced to use the mouse; I prefer to use the keyboard as much as I can. Also, it’s far too easy to accidentally wipe out the primary selection by selecting some other text before you complete the paste.
Each of these text buffers is assigned to a special register in Vim: the primary selection is called register *
and the clipboard is called +
. This means I can get my text snippet into the clipboard by yanking
into register +
with “+y
. Then I can switch to the other application and paste with ctr-v
with no detour to the mouse, and no worries about wiping out the buffer. This is good, but it’s too much typing. So I have this in my .vimrc file: