Learn Practice Daily golf Reference

How to copy to the system clipboard in Vim

Type "+y and a motion ("+yy for the line) to copy to the system clipboard, and "+p to paste from it. Or add set clipboard=unnamedplus to your config so plain y and p use it.

Vim's y and p use Vim's own register, not the clipboard your other programs share. That's why a line yanked in Vim doesn't paste into your browser, and why p doesn't paste what you just copied there. Two registers reach the system clipboard: + and *. Name the register before the command, with a double quote: "+y copies to the clipboard, "+p pastes from it.

On Linux, + is the clipboard that Ctrl-C and Ctrl-V use, and * is the primary selection: whatever you last highlighted, the text a middle-click pastes. On macOS and Windows there is only one clipboard, and + and * both mean it.

Try it here

Copy the error into the report

Select the two lines of the error with V and j, copy them with "+y, then go to the end with G and paste with "+p. In this page the clipboard is simulated and stays inside the editor; in your own Vim, the same keys reach every other program.

Copy the two error lines with "+y, paste them at the end with "+p
1Error: connect ECONNREFUSED 127.0.0.1:5432
1 at TCPConnectWrap.afterConnect (node:net:1555:16)
2
3Bug report:
+Error: connect ECONNREFUSED 127.0.0.1:5432
+ at TCPConnectWrap.afterConnect (node:net:1555:16)
NORMALLoading the editor…0.0s1:1

This editor needs a keyboard. Read on, and play it at your computer.

KeysWhat it does
"+yyCopy the line to the system clipboard.
"+yiwCopy the word under the cursor.
V or v, then "+yCopy what you selected.
"+p / "+PPaste from the clipboard after / before the cursor.
:%y+ ↵Copy the whole file to the clipboard.
Ctrl-r+In Insert mode: paste the clipboard where you are typing.
"*y / "*pOn Linux, copy to and paste from the primary selection instead.
:set clipboard=unnamedplus ↵Make every yank, delete and paste use the clipboard until Vim closes (in Vim on macOS and Windows, clipboard=unnamed). Put it in your config to keep it.

Make y and p use the clipboard

If you'd rather every yank went to the clipboard, set the clipboard option once in your config. In Vim on Linux, add set clipboard=unnamedplus to ~/.vimrc; in Vim on macOS and Windows, set clipboard=unnamed (unnamedplus needs Vim's X11 or Wayland clipboard support). In Neovim, vim.opt.clipboard = 'unnamedplus' in init.lua works everywhere.

The cost: deletes go to the clipboard too, so dd or x replaces whatever you copied in your browser a moment ago.

When nothing reaches the clipboard

Vim can only reach the clipboard if it was built with clipboard support. Run vim --version in a terminal and look for +clipboard (supported) or -clipboard (not); inside Vim, :echo has('clipboard') prints 1 when it's there. Some Linux packages leave it out: on Debian and Ubuntu, the vim-gtk3 package has it.

Neovim has no build flag. It passes the text to a clipboard tool: pbcopy and pbpaste on macOS, which are always there; on Linux, wl-copy (from wl-clipboard) under Wayland, or xclip or xsel under X11; win32yank on Windows. :checkhealth says which one it found. Without one, Neovim 0.10 and later can use the terminal's own clipboard support (OSC 52) instead, if the terminal has it, which works over SSH too; it looks for it only while the clipboard option is unset.

Pasting while you type

In Insert mode, Ctrl-r then + inserts the clipboard where you're typing, without going back to Normal mode. It inserts the text as if you'd typed it, so auto-indent can still add to the indentation of each line. Ctrl-rCtrl-o+ inserts it literally and doesn't indent it.