If you are new to Vim, you might be surprised when you try to copy bits of text from your editor to paste them into other applications. I remember I was. Observing from another application, simply yanking a line will seemingly end up nowhere. Of course, there are several options to do this essential task, but there are a few of gotchas along the way.
The System Clipboard
If you use Vim as your primary editor, the most convenient way to copy around is to yank text directly to the system clipboard.
Living in the 21st century, you are most likely accustomed to the fact that there is only one clipboard
to store the stuff that you
Ctrl + c. This is not the case in Vim. Vim has multiple registers where you
can store information separately. Each register has a unique identifier. That identifier has to be
referred when you wish to store or retrieve information:
"<register id>y: yank (copy) to register
"<register id>p: paste from register
For example, paste to, then copy from the
r register with
Conveniently Vim allows to omit the
" and the register identifier, so you can simply use
In these cases, the data will be accessed from the default (
For our purposes, we need the
+ register, as it is special in the sense that it’s associated with the
system clipboard. The content yanked to it will be accessible to other applications. Similarly, you can
use it to paste text copied to the clipboard by other applications.
To work with it, essentially you have two options:
- Set the
+register as the default:
:set clipboard=unnamedplus. After this, every time you simply
p, Vim will use the system clipboard.
- Yank to the system clipboard explicitly only when you need it with
"+y, and paste from it with
There is one caveat though, Vim has to be compiled with clipboard support for this to work, and many
distros come with
vim package that does not have this feature.
Check the clipboard support of your Vim with
vim --version and look for the
If you see either of those, then your Vim supports the access to system clipboard and you are good to go.
If not, you better look for an alternative package, like
vim-gtk on Debian or
vim-X11 on Red Hat Linux.
Check out this VimTip for more info on this topic.
Plan B: Use terminal features
If you are stuck with a version that does not come with this feature you still have some options.
The pasting part is pretty trivial: just jump into
insert mode and press
Shift + Insert. If you are inserting
source code, it might worth enabling paste mode with
:set paste to preserve indentation. (You can disable paste
:set nopaste later.)
Copying can be a bit trickier as Vim might try to handle the mouse on its own. Most terminal emulators have the option to capture mouse interactions rather than passing it to the running application. You can take advantage of that to copy a piece of text.
For example with Konsole simply hold down the
Shift key then select and copy the text from the terminal, as
you would normally do. Of course, if you didn’t enable mouse support, then you can simply copy text from the
terminal as usual.
As a last resort, you can use
:!cat % to pipe the currently edited file to cat, displaying it entirely outside
of vim. In this case, it’s easy to copy multiple pages as well.