131

I'm trying to copy-paste some text from vim. I'm doing v to enter visual mode, then y once I selected my block.

It appears to copy the text into vim's clipboard, because p will paste it. But in another program (e.g. Chrome), right-click->paste doesn't paste the correct text. How do I copy text to the correct clipboard?

Braiam
  • 35,991
ripper234
  • 31,763

12 Answers12

103

The following will work only if vim --version indicates that you have +xterm_clipboard feature. If not, you will have to install extra packages or recompile vim with that feature added.


There are actually two options for this:

"+y

copies to the "usual" clipboard buffer (so you can paste using Ctrl+V, right click and select "Paste" etc), while

"*y

copies to the X11 selection - you can paste from this buffer using middle click.

Note that "* and "+ work both ways. So if you have selected some text in another application, you can paste it into vim using "*p and if you have copied some text (using, say, Ctrl-C) then you can paste it into vim using "+p.

tshepang
  • 65,642
  • 2
    @Tshepang it is not working from putty. what should I do ? – Rahul Patil Apr 10 '13 at 02:54
  • @RahulPatil I know not; maybe ask a separate question, perhaps on [su] (since it's a Windows tool, it would be more welcome there). – tshepang Apr 10 '13 at 05:19
  • 1
    @RahulPatil - if you are using putty then you are editing the file on a remote server, not on your local machine. Generally the remote server won't know about the clipboard on your local machine. If you were using a Linux desktop then you can ssh with X forwarding (provided you have installed the X libraries on the server), but as putty runs on windows you don't have X running so you can't forward it. – Hamish Downer Apr 10 '13 at 11:32
  • 3
    Hello everybody, How to type "+y ? should i type it in Visual mode or in command mode ? Silly thing, but i am unable to figure it out. – Alind Billore Jun 15 '16 at 12:38
  • 1
    @AlindBillore: In command mode. Literally, press the keys shift' then shift= then y (not all at once, one after the other, except the shifts of course). – Reid Mar 30 '17 at 18:39
  • 1
    I have +clipboard and -xterm_save . How do I get the +xterm_clipboard? – Jdeep Jul 22 '20 at 08:04
  • This answer requires gvim, but more importantly will not work if your SSH'ed into a host, even with gvim installed locally on your client system and the server system. – Dave Apr 05 '23 at 16:57
64

Hold down the shift key and select text with the mouse cursor.
Text will be copied to the clipboard.

This also works for pasting from the clipboard in to vim.

To be clear, this works for vim running in a terminal such as xterm.

41

If you are using vim >=7.3.74, then you can actually put this in your vimrc:

set clipboard=unnamedplus

Which will automatically use the + buffer (the system clipboard) by default. Then to yank (copy), you just use the regular y command, etc.

I found this behavior to be fairly annoying, though, as commands like d put the text they operate on into the default buffer, which meant I couldn't do a fast dd and paste a line from the internet, for example.

Reid
  • 577
  • 2
    Regarding your last point, dd means "cut one line", so that would be the expected behavior. In the rare case when you want to actually delete a line into oblivion, "_dd will do the trick. – user7089 Apr 24 '14 at 00:38
  • 2
    also as @HamishDowner notes in his answer: The following will work only if vim --version indicates that you have +xterm_clipboard feature. If not, you will have to install extra packages or recompile vim with that feature added. – rudolph9 Mar 30 '17 at 15:51