Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy to clipboard in Vim?

Tags:

vim

clipboard

Is it possible to copy to clipboard directly from Vim? yy only copies stuff to Vim's internal buffer. I want to copy to the OS's clipboard. Is there any such command in Vim or you can only yank stuff within Vim?

like image 421
Hari Menon Avatar asked Oct 18 '10 17:10

Hari Menon


People also ask

How do I copy and paste from clipboard in Vim?

When using Vim under Windows, the clipboard can be accessed with the following: In step 4, press Shift+Delete to cut or Ctrl+Insert to copy. In step 6, press Shift+Insert to paste.

How do I use clipboard in Vim?

To work with it, essentially you have two options: Set the + register as the default: :set clipboard=unnamedplus . After this, every time you simply y or p , Vim will use the system clipboard. Yank to the system clipboard explicitly only when you need it with "+y , and paste from it with "+p .

How do I enable copy and paste in Vim?

You can use :set mouse& in the vim command line to enable copy/paste of text selected using the mouse. You can then simply use the middle mouse button or shift insert to paste it. Save this answer.


1 Answers

The * register will do this. In Windows, + and * are equivalent. In unix there is a subtle difference between + and *:

Under Windows, the * and + registers are equivalent. For X11 systems, though, they differ. For X11 systems, * is the selection, and + is the cut buffer (like clipboard). http://vim.wikia.com/wiki/Accessing_the_system_clipboard

* is probably what you want most of the time, so I use * because it functions as I expect it to in both environments.

In Linux distros you have to install vim-gtk (aka gvim) first to gain clipboard functionality. This is because non-gtk vim is typically compiled without X11 support. This is to allow it to run on console only machines (often servers).

And for those confused about how to use registers when yanking or putting, you merely write " then the name of the register. So for copying something to the clipboard register you type "*y and then to put you type "*p (credit: Kyle Mathews)

like image 193
Gabe Moothart Avatar answered Oct 04 '22 04:10

Gabe Moothart