12

I can cat a file, copy and paste text with the middle button or shift-insert.

How can I copy text in vim and paste it from the system clipboard?

3
  • sometimes I use sed -n5,10p to print lines 5-10 so I can more easily copy and paste without vim reacting to the mouse.
    – grantbow
    Oct 25, 2016 at 20:53
  • actually from inside vim the command looks like :!sed -n 5,10p %
    – grantbow
    Nov 29, 2016 at 21:59
  • Thanks for all the comments and answers. xclip is useful sometimes, thanks @sergiy-kolodyazhnyy. When I work on different machines I sometimes do not have control over which vim is installed but having the instructions for checking @muru 's answer is helpful, thanks @ashutosh-tripathy. I need to use the vim register commands more often to remember them when I need them. As I mentioned in the question above, if I want to do it quickly and the file is small the command inside vim looks like :!cat %
    – grantbow
    Oct 15, 2018 at 18:03

4 Answers 4

13

You need to have Vim with the clipboard and xtermclipboard features compiled in. In Ubuntu, these are only available with the vim GUI packages (vim-gnome, vim-gtk, vim-athena, etc.).

Once you install one of these, you can copy to (and paste from) the clipboard registers (* and +). From this very informative post on Vi and Vim:

For X11-based systems (ie. Linux and most other UNIX-like systems) there are 2 clipboards, which are independent of each other:

  • PRIMARY - This is copy-on-select, and can be pasted with the middle mouse button.
  • CLIPBOARD - This is copied with (usually) ^C, and pasted with ^V (It's like MS Windows).

Vim has 2 special registers corresponding to these clipboards:

  • * uses PRIMARY; mnemonic: star is select (for copy-on-select)
  • + uses CLIPBOARD; mnemonic: CTRL + C (for the common keybind)

To copy to a register, you precede the copy command (y) with " and the name of the register (*, for example). "*y, then middle-click to paste, or "+y and ShiftInsert to paste.

5

I had issue because 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 (which support clipboard) 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
  • 1
    in ubuntu 20 this no longer works -had to install vim-gtk
    – Goblinhack
    May 28, 2021 at 7:53
4

A quite interesting solution comes from this question. Install xclip, then pipe output of a command to xclip( cat file | xclip -selection clipboard for instance ), and then paste it anywhere, (if that's in terminal - use Ctrl ShiftV ). Now , turns out you can actually shorten that command to xclip -sel clip, which is not exactly apparent from reading man page or examples there.

3
  • 2
    Another way out is if you use screen or tmux, along with this plugin. In any case, something needs to be installed.
    – muru
    Apr 24, 2015 at 6:45
  • What's the actual clipboard that is used in x server ? I didn't find anything in /dev, but text is still held somewhere, right ? Apr 24, 2015 at 6:52
  • I always assumed it would be in memory. That seems like a nice question for Unix & Linux, though.
    – muru
    Apr 24, 2015 at 7:15
0

Building up on Sergiy Kolodyazhnyy's answer, if you have compiled vim without xclip support, then do the following to copy content from vim to the system clipboard:

  • Install xclip (sudo apt install xclip)
  • Yank the required lines - this will be stored in the "0 register
  • Then do :call system('xclip -sel clipboard', @0) to copy the contents to your clipboard.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .