119

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?

0

11 Answers 11

94

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.

6
  • 2
    @Tshepang it is not working from putty. what should I do ? Apr 10, 2013 at 2:54
  • 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. Apr 10, 2013 at 11:32
  • 2
    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. Jun 15, 2016 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, 2017 at 18:39
  • 1
    I have +clipboard and -xterm_save . How do I get the +xterm_clipboard?
    – Jdeep
    Jul 22, 2020 at 8:04
53

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.

6
  • 2
    @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 Jan 24, 2018 at 14:34
  • 1
    On MAC hold down the option key and select text with the mouse cursor
    – abreschi
    Oct 4, 2019 at 21:26
  • Doesn't work for me using vim 8.0 + gnome-terminal
    – jchook
    Oct 26, 2019 at 21:09
  • Solution not compatible with multipane terms like tmux. Jul 11, 2020 at 13:18
  • This does not copy to the clipboard, this copies to the primary. See the distinction in Clipboard Arch Wiki article.
    – Quasímodo
    Aug 4, 2020 at 17:15
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.

3
  • 2
    + is a clipboard regiser
    – TheOneTeam
    Aug 8, 2013 at 8: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, 2014 at 0: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, 2017 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
9

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.

1
  • For Ubuntu 20.04 instead install vim-gtk3
    – an4s911
    Sep 13, 2021 at 9:19
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

3
  • That does not work.
    – Astrid
    Jun 14, 2017 at 16:57
  • creative answer
    – vidur punj
    Nov 8, 2017 at 12:00
  • For me it worked fine. Hold shift key before starting selecting. The selection gets a different color, the vim bar will not be "VISUAL" and the terminal menu will enable the Copy option. Apr 15, 2021 at 2:20
6

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

"+y

Very intuitive.

3
  • Are you using gvim?
    – nozimica
    May 3, 2011 at 16:14
  • @nozimica- yeah
    – ripper234
    May 3, 2011 at 17:09
  • 1
    That's also available in vim running in an xterm or similar terminal emulator under X (if vim is compiled with the +xterm_clipboard feature). May 3, 2011 at 21:02
6

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.

4
  • 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 3, 2011 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 3, 2011 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 4, 2011 at 2:41
  • if you use shift-control mousedrag - select text, copy pasting from one buffers window becomes easy.
    – Stephan
    Nov 3, 2011 at 10:33
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.

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.

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.