127

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,380
  • 25
  • 108
  • 167
ripper234
  • 31,063
  • 43
  • 82
  • 90

12 Answers12

101

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
  • 64,472
  • 86
  • 223
  • 290
Hamish Downer
  • 1,669
  • 1
  • 12
  • 11
  • 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
59

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.

Andrzej Rehmann
  • 183
  • 1
  • 5
Andrew Stacey
  • 2,355
  • 3
  • 19
  • 21
  • 3
    @LoopSpace for multiline selections this will also copy line numbers too when `set nu` is enabled. So u would probably want to toggle line numbers with some shortcut or run the `:set nonu` command before every selection – Steven Pribilinskiy Jan 24 '18 at 14:34
  • 1
    On MAC hold down the option key and select text with the mouse cursor – abreschi Oct 04 '19 at 21:26
  • Doesn't work for me using vim 8.0 + gnome-terminal – jchook Oct 26 '19 at 21:09
  • Solution not compatible with multipane terms like tmux. – Adrian Lopez Jul 11 '20 at 13:18
  • This does not copy to the clipboard, **this copies to the primary**. See the distinction in [Clipboard Arch Wiki article](https://wiki.archlinux.org/index.php/Clipboard#Selections). – Quasímodo Aug 04 '20 at 17:15
  • In vim, holding down shift, selecting the contents and using Konsole's copy shortcut (Ctrl+Shift+C) worked for me, thank you. – NuclearPeon Sep 02 '20 at 21:15
  • Another side effect of doing it this way is it copies ALL white space to the right or any line if your copying multiple lines, not ideal. @adrian Lopez, you must zoom into the specific pane you want to copy from first. – Dave Apr 05 '23 at 16:50
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
  • 3
  • 11
  • 2
    + is a clipboard regiser – TheOneTeam Aug 08 '13 at 08:23
  • 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
15

I find the standard "+gP and "+y commands very difficult to use. So I've remapped Ctrl-Y for yanking into the clipboard and Ctrl-P to paste from the clipboard. Add this to .vimrc

nnoremap <C-y> "+y
vnoremap <C-y> "+y
nnoremap <C-p> "+gP
vnoremap <C-p> "+gP
Bernhard Heijstek
  • 1,022
  • 3
  • 10
  • 22
10

When I checked my vim was not supporting clipboard:

vim --version | grep clip
-clipboard       +insert_expand   +path_extra      +user_commands
+emacs_tags      -mouseshape      +startuptime     -xterm_clipboard

I installed vim-gnome and then checked again

vim --version | grep clipboard
+clipboard       +insert_expand   +path_extra      +user_commands
+emacs_tags      +mouseshape      +startuptime     +xterm_clipboard

Now I am able to copy and paste using "+y and "+p respectively.

me_astr
  • 203
  • 2
  • 5
8

Just to add a non-vim-specific answer: You can use Ctrl+Shift+C to copy to the system clipboard in Ubuntu (if the functionality is provided by your terminal emulator, gnome-terminal for one provides it). Ctrl+Shift+V pastes to another terminal window, regular Ctrl+V pastes to a GUI app.

Additionally, if you select the text in the terminal, then middle-click, it will be pasted from the X11 clipboard.

crazy2be
  • 181
  • 3
  • For some reason it doesn't work. Just select with visual mode, copy with ctrl-shift-C, then paste in another app using Ctrl-shift-V ? The text doesn't get copied for me. – ripper234 May 03 '11 at 23:14
  • 2
    @ripper234: this does not work if you are using gvim. As crazy2be said, this functionality is from your terminal emulator. – Reid May 03 '11 at 23:20
  • @ripper234: 1) It is functionality provided by your terminal emulator (gnome-terminal for one provides it), 2) Ctrl+Shift+V is to paste *to the terminal*, GUI apps still use Ctrl+V as per usual. I'll edit the answer to make it more clear. – crazy2be May 04 '11 at 02:41
  • if you use shift-control mousedrag - select text, copy pasting from one buffers window becomes easy. – Stephan Nov 03 '11 at 10:33
8

Hold down the SHIFT key and mouse select text to copy it.

Then paste in other program with CTRL + v.

Works in terminal on my Ubuntu 16.04. Not sure about gvim and other systems.

enter image description here

Andrzej Rehmann
  • 183
  • 1
  • 5
6

Never mind, I found the answer - it's written on the vim's menus.

"+y

Very intuitive.

ripper234
  • 31,063
  • 43
  • 82
  • 90
2

By default, if you install the light version of vim, then you will not have the +xterm_clipboard feature on (vim --version | grep xterm), which is needed for using the * and + registers outside of vim.

To enable it, you must either compile vim yourself, or install a version of vim that comes with it preenabled. I used vim-gnome: sudo apt-get install vim-gnome. If you perform vim --version | grep xterm, you will see that it is now +xterm_clipboard.

Now, try copying to the + and * and it should work.

laughing_man
  • 209
  • 2
  • 2
1

This answer is for macOS users:

I'm adding this because none of the other answers that I tried here worked for me... but this one does! I cannot recall where I found this; I can't claim credit, but I will claim that it works for macOS, and it seems to be "native" to vim so it should work in other systems also:

  1. From vim in your terminal window, enter visual mode by typing v

  2. Select the text you want to copy using the mouse or glidepad

  3. Type "*y; the selected text is copied to the system clipboard

You may now paste the copied text wherever it's needed.

IIRC, " tells vim to select a register; * specifies the system register; y is the yank (copy) command.

Seamus
  • 2,522
  • 1
  • 16
  • 31
0

For Ubuntu 14.04 And Above:

  • Just select the text which you wanna be copy. And then click the centre roll button with your mouse and Go to the clipboard

  • And do the same there too (click the centre roll button with your mouse).

It's 100% working in both vim and tail on Ubuntu.

AdminBee
  • 21,637
  • 21
  • 47
  • 71
0

Just to add, if anything works try to switch the terminal that wraps vim (if you are using vim from the terminal). I found that, for example, the clipboard is inaccessible by guake while is accessible from gnome-terminal.

gc5
  • 369
  • 4
  • 8