Is there a way to copy a block of text to the system clipboard, so I can paste it in another program?

up vote 209 down vote accepted

For X11-based systems (ie. Linux and most other UNIX-like systems) there are two 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).

OS X and Windows systems only have one clipboard.

For X11 systems there are also number of tools that synchronize these clipboards for you; so if they appear to be the same, you may have one of them running.

Vim has two special registers corresponding to these clipboards:

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

On Windows & OS X there is no difference between + and *, since these systems only have a single clipboard, and both registers refer to the same thing (it doesn't matter which one you use).

You can use these registers as any register. For example, using the PRIMARY clipboard * with the y and p commands:

  • "*yy
  • "*p

You could maybe use this as more convenient keybinds:

noremap <Leader>y "*y
noremap <Leader>p "*p
noremap <Leader>Y "+y
noremap <Leader>P "+p

If you want to "automatically" interface with the system's clipboard instead of referring to it manually all the time, you can set the clipboard variable:

  • Set it to unnamed to use * (PRIMARY, on select)
  • Set it to unnamedplus to use + (CLIPBOARD, ^C)

Now, just using yy will go to the system's clipboard, instead of Vim's unnamed register, and p will paste the system's clipboard.

You can also assign to these registers just like any register with let:

  • :let @+=42
  • :let @*=42

The clipboard setting has some more options (such as exclude filters); but these are the basics. See :help 'clipboard' for the full story ;-)

gVim

If you use gVim, you can get copy-on-select behaviour when using :set guioptions+=a.
This is enabled by default on X11 systems (copies to PRIMARY), but not on MS Windows & OSX (as selecting any text would override your clipboard).

No +clipboard?

Vim requires the +clipboard feature flag for any of this to work; you can check if your Vim has this by using :echo has('clipboard') from within Vim (if the output is 0, it not present, if it's 1, it is), or checking the output of vim --version.

Most Linux distributions ship with a "minimal" Vim build by default, which doesn't have +clipboard, but you can usually install it:

  • Debian & Ubuntu: Install vim-gtk or vim-gnome.
  • Fedora: install vim-X11, and run vimx instead of vim (more info).
  • Arch Linux: install gvim (this will enable +clipboard for normal vim as well).

You could also use xclip, xcopy, or xsel to copy text to the clipboard; see the following questions for solutions:

SSH

You can also use a clipboard on remote machines if you enable X11 forwarding over SSH. This is especially useful with the above tip since you can then use xclip to access your desktop's clipboard. The Vim on the machine you're ssh-ing to will still need the +clipboard feature.

This requires the ForwardX11Trusted setting, and should only be done with trusted servers, as this gives the server almost complete control over your X11 session:

$ ssh -XY myhost

To make these settings persistent (so you don't need to add -XY every time), you could do something like this in your ~/.ssh/config:

# Do **NOT** set this globally; it gives the server complete control over
# your X11 session.
Host myhost
    ForwardX11 yes
    ForwardX11Trusted yes

Neovim

Neovim revamped the clipboard support. The built-in interface was removed and replaced with a system that call an external utility such as xclip, xsel, or pbcopy/pbpaste.

It should automatically pick up these utilities and use them. On OS X pbcopy and pbpaste should be available by default, on Linux you probably want to install xclip, as that's the most widely available (there are actually two versions of xsel with incompatible flags. This is really stupid).

Also see :help clipboard in Neovim.

  • 5
    ssh -Y implies -X so -X can be omitted. – hildred Mar 16 '15 at 20:52
  • For some international keyboards, you may need to press "<Space> to get a ". So in those case you would have to press "<Space>+y or "<Space>*y to copy. – Arthur F Aug 23 '15 at 18:53
  • A very interesting side effect of learning this (a few years ago) for me has been the realization that " can select many named registers. "+ is not magic, it's just register +. So for example, if you want to copy three things at the same time and paste each of them one by one, you can "1y, "2y and "3y and later "1p, "2p and "3p. This is really powerful and non-existent in almost any other editor. Another interesting usage is to look at stored macros. For example, if you record with qq, but you realize you need to fix it, you can: in a temp line "qp, fix it, and "qd. – Shahbaz Feb 3 '17 at 18:06
  • Just a note: OS X has two separate clipboards. One is accessed using Cmd-C and Cmd-V and the other using the emacs keybindings Ctrl-K and Ctrl-Y. – BallpointBen Feb 5 '17 at 17:16
  • 2
    @shahbaz -- probably better to use the letter registers for such a purpose, since the numbered ones get changed whenever text is deleted. – evilsoup Feb 26 '17 at 10:17

The other answers cover how to copy text from your buffer into the system clipboard. Another common operation is to copy text from another register to the clipboard. For example, if you've already yanked some text into " (the default register), you might want to load that register into the clipboard.

You can do this with :let:

  • let @+=@" — copies the default register into the clipboard
  • let @*=@" — copies the default register into the X11 primary selection ("mouse clipboard")
  • let @+=@a — copy from register a to the clipboard
  • etc.

Note that this works for registers in general: let @a=@b copies register b to register a.

From http://vim.wikia.com/wiki/Accessing_the_system_clipboard

set clipboard=unnamedplus

This changes the default Vim register to the + register, which is linked to the system clipboard. From :h clipboard-unnamedplus:

                                        clipboard-unnamedplus
unnamedplus     A variant of the "unnamed" flag which uses the
                clipboard register '+' (quoteplus) instead of
                register '*' for all yank, delete, change and put
                operations which would normally go to the unnamed
                register.  When "unnamed" is also included to the
                option, yank operations (but not delete, change or
                put) will additionally copy the text into register
                '*'.
                Only available with the +X11 feature.
                Availability can be checked with: 
                        if has('unnamedplus')

On builds that support it, the register named * is the system clipboard. To copy text from Vim to the system clipboard, you can select the text using visual mode, then press "*y to copy it to the system clipboard.

Conversely, use "*p to paste text from the system clipboard into Vim.

I recommend you use Gvim, and add this to your .vimrc:

source $VIMRUNTIME/mswin.vim
behave mswin

This enables support for CtrlC, CtrlX, CtrlV like notepad in Windows.

Note that this will also add some other common Windows shortcuts, such as CtrlS for :w, CtrlA for select all, etc.

If your vim does not support +clipboard you can use the fakeclip plugin for Linux, Mac, Windows, tmux, screen, ...

Every other answer has covered all the important things about the registers. I would like to add one more thing that I use.

There is a way in Vim to directly copy the visually selected text in to the clipboard. Just put this in your vimrc file:

set guioptions+=a

And, with this whenever you visually select some text it will be copied to your clipboard. For more information on this, :help guioptions

BTW, this is only for gVim.

  • 2
    guioptions+=a is also mentioned in Carpetsmoker's answer. – muru Feb 26 '16 at 7:32
  • @muru Yeah, sorry. I didn't see that before. I just saw it. – Durga Swaroop Feb 26 '16 at 8:26

protected by Martin Tournoij Feb 25 '17 at 9:22

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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