Subscribe:   RSS icon   twitter icon

Vim: Yank Into Clipboard

Lee Phillips
February 24, 2014; updated February 26 with a far better approach.

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: set clipboard=unnamedplus This magic command tells vim to use the “plus” register as the clipboard when yanking (as well as the “unnamed” register), which has the desired effect. The fact that this wipes out my clipboard doesn’t bother me, because when I’m in Vim I don’t care what’s on the clipboard unless I’m about to leave Vim to paste something somewhere else.


Share with Facebook Share with Twitter Share with Reddit Share with StumbleUpon Share with Digg Share with Slashdot
▶ Comment
lee-phillips.org is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to amazon.com.
Quotilizer ... loading ...

Tenuously related:

The best tool for CVs with publication lists.