This tip is very similar to the following:
These tips need to be merged – see the merge guidelines.
created 2005 · complexity basic · author JimD · version 7.4
When performing copy, cut, and paste with commands like
p, by default Vim uses its own location for this, called the unnamed register (:help quotequote). Note that this is different from what most modern graphical text editors and other applications like web browsers do; these applications interact with the system clipboard when using keybindings like CTRL-C, CTRL-X, and CTRL-V. Fortunately, in most cases it is easy to get Vim to work with the system clipboard.
Vim offers the
* registers to reference the system clipboard (:help quoteplus and :help quotestar). Note that on some systems,
* are the same, while on others they are different. Generally on Linux,
* are different:
+ corresponds to the desktop clipboard (
XA_SECONDARY) that is accessed using CTRL-C, CTRL-X, and CTRL-V, while
* corresponds to the X11 primary selection (
XA_PRIMARY), which stores the mouse selection and is pasted using the middle mouse button in most applications. We can use these registers like any other register. Here are a few common commands that demonstrate this:
gg"+yG– copy the entire buffer into
"*dd– cut the current line into
"+p– paste from
+after the cursor (works in both normal and visual modes)
:%y *– copy the entire buffer into
*(this one is an ex command)
One distinction to make is that using
* is different from using CTRL-SHIFT-V on the terminal (or doing a right-click and then selecting "paste" in the terminal menu), where Vim essentially inserts each character in the clipboard one-by-one. In general using
* is much more reliable than using CTRL-SHIFT-V.
See also pasting registers for more general information about pasting from registers.
I searched though the tips and found only a few passing references to this feature: VimTip71 (implies it's a feature of gvim, but I find it works in console Vim just fine under Linux; I only use gvim under MS Windows). VimTip771 and VimTip964 refer to an extra utility named xclip which is only for X11 and seems to be completely unnecessary for Vim using the
* register), and VimTip960 (misses the point but the comments mention it a few times).
VimTip21 is spot on but the title suggests it's for MS Windows only. It also mentions that we can change out settings so the "anonymous" register is aliased to the
* register using:
- Note: in vim 7.3.74 and higher you can set
clipboard=unnamedplusto alias unnamed register to the
+register, which is the X Window clipboard.
VimTip432 and VimTip600 use the
* register in some native Vim code (using
@*) without explaining it in general. VimTip448 also uses it (for converting hex to decimal) but explains it in passing. VimTip478 uses it in a function for copying the results of a
:g search into the
* register. VimTip687 suggests that Mac OS X doesn't implement the * register (yet?) but suggests a workaround using the pbcopy and pbpaste utilities. VimTip876 also makes passing reference to it.
There are several tips for copying the current filename into the clipboard including: VimTip432, VimTip891, VimTip600 (most of the fuss is resolving forward slashes and backslashes for MS Windows paths).
Getting Vim to work with the X11 clipboard can be a struggle if you want to run Vim in a terminal. In this case, you will have to check for X11 clipboard support. The GUI version of Vim always has clipboard support.
From the console, type:
vim --version | grep clipboard
If you see
+xterm_clipboard, you are good to go. If it's
-xterm_clipboard, you will need to look for a version of Vim that was compiled with clipboard support. On Debian and Ubuntu, to obtain clipboard support install the packages
vim-gnome (not vim-tiny).
If you are running Redhat/CentOS, you can install the vim-X11 package (if you have gvim then this is already installed). This provides the vimx command, which is a console version of Vim with X11-clipboard support. Or if you are running Debian (only test on 7.0), installing vim-gnome package fixes the problem. I like aliasing Vim to vimx by adding this line to my .bashrc/.cshrc:
.bashrc or .zshrc: if [[ -x "$(command -v vimx)" ]]; then alias vim='vimx'; fi .cshrc: if (-e /usr/bin/vimx) alias vim '/usr/bin/vimx'
If you are running Gentoo, emerge vim with the 'X' USE flag. For instance, adding this line to /etc/portage/package.use:
There is a convenient workaround to create a few commands for pasting/copying selections into/out of various X11 clipboards with `xsel`.
:command -range Cz :silent :<line1>,<line2>w !xsel -i -b :command -range Cx :silent :<line1>,<line2>w !xsel -i -p :command -range Cv :silent :<line1>,<line2>w !xsel -i -s :cabbrev cv Cv :cabbrev cz Cz :cabbrev cx Cx :command -range Pz :silent :r !xsel -o -b :command -range Px :silent :r !xsel -o -p :command -range Pv :silent :r !xsel -o -s :cabbrev pz Pz :cabbrev px Px :cabbrev pv Pv
With this code blob dumped in your ~/.vimrc you can do the following
1. Select a blob of text in visual mode 2. Step into command line with ':' 3. :`<,`>cz
and your selected text will be copied into one of X's 3 default clipboards.
there are 6 commands, 3 for pasting, 3 for copying, and each command represents a buffer.
z = "X11-Clipboard" x = "X11 Primary Selection" v = "X11 Secondary Selection"
Make sure your DISPLAY environment variable is set appropriately - otherwise vim can not connect to your x-session to access the clipboard.
To check run:
which should output something like:
If nothing work and you would like to copy from vim (under xterm) to leafpad, you may use this add-on to your ~/.vimrc
Press F9, and it will copy to the x11 clipboard.
function Func2X11() :call system('xclip -selection c', @r) endfunction vnoremap <F9> "ry:call Func2X11()<cr> vnoremap <m-c> "ry:call Func2X11()<cr> vnoremap <ESC-c> "ry:call Func2X11()<cr>
I hope that it may help you.
:version shows "
+xterm_clipboard", but using the clipboard still does not work, try using other system tools as a workaround. For example, you can search for a clipboard.sh script which can be used like '
cat a.txt | clipboard' to copy the contents of file a.txt to the system clipboard. When using kubuntu, the system clipboard is managed by 'Klipper', you can use 'Klipper' shortcut to paste anything to the system clipboard from anywhere because its shortcut has the highest precedence. --Changc11, October 10, 2013
- Has a case like this been discussed at the vim_use mailing list? If clipboard support is provided by Vim, and if the versions of Vim and the system are not excessively old, the clipboard should work. This idea is better as a comment until something more conclusive is known. JohnBeckett (talk) 10:27, October 13, 2013 (UTC)