The best answers to the question “How to copy to clipboard in Vim?” in the category Dev.
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?
On Mac OSX
copy selected part: visually select text(type
mode) and type
copy the whole file
paste from the clipboard
On most Linux Distros, you can substitute:
xclip -i -sel cor
xsel -i -b
xclip -o -sel -cor
xsel -o -b
— Note: In case neither of these tools (
xclip) are preinstalled on your distro, you can probably find them in the repos
* register will do this. In Windows,
* are equivalent. In unix there is a subtle difference between
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).
* 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
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)
Use the register
"+ to copy to the system clipboard (i.e.
"+y instead of
Likewise you can paste from
"+ to get text from the system clipboard (i.e.
"+p instead of
In your vimrc file you can specify to automatically use the system clipboard for copy and paste.
On Windows set:
On Linux set (vim 7.3.74+):
NOTE: You may need to use an up to date version of Vim for these to work.