Is there a way to copy a block of text to the system clipboard, so I can paste it in another program?
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
*, 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
* with the
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
- Set it to
*(PRIMARY, on select)
- Set it to
Now, just using
yy will go to the system's clipboard, instead of Vim's unnamed
p will paste the system's clipboard.
You can also assign to these registers just like any register with
clipboard setting has some more options (such as exclude filters); but
these are the basics. See
:help 'clipboard' for the full story ;-)
If you use gVim, you can get copy-on-select behaviour when using
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).
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
0, it not present, if it's
1, it is), or checking the output of
Most Linux distributions ship with a "minimal" Vim build by default, which
+clipboard, but you can usually install it:
- Debian & Ubuntu: Install
- Fedora: install
vim-X11, and run
- Arch Linux: install
gvim(this will enable
You could also use
xsel to copy text to the clipboard;
see the following questions for solutions:
- Define custom commands for the * and + registers
- How can I copy over an ssh session without +clipboard?
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
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
# Do **NOT** set this globally; it gives the server complete control over # your X11 session. Host myhost ForwardX11 yes ForwardX11Trusted yes
Neovim revamped the clipboard support. The built-in interface was removed and
replaced with a system that call an external utility such as
It should automatically pick up these utilities and use them. On OS X
pbpaste should be available by default, on Linux you probably want to
xclip, as that's the most widely available (there are actually two
xsel with incompatible flags. This is really stupid).
:help clipboard in Neovim.
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 @+=@"— copies the default register into the clipboard
let @*=@"— copies the default register into the X11 primary selection ("mouse clipboard")
let @+=@a— copy from register
ato the clipboard
Note that this works for registers in general:
let @a=@b copies register
b to register
This changes the default Vim register to the
+ register, which is linked to the system clipboard. From
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
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:
And, with this whenever you visually select some text it will be copied to your clipboard. For more information on this,
BTW, this is only for gVim.
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?