How do I copy text to the system clipboard in Vim?
Answer
"+y
Explanation
The "+y command yanks (copies) text into the system clipboard register, making it available to paste in other applications outside of Vim. By default, Vim's yank and delete operations use internal registers that are not shared with the operating system clipboard.
How it works
"tells Vim you are about to specify a register+is the system clipboard register (the X11 clipboard on Linux, or the system clipboard on macOS and Windows)yis the yank operator, which can be combined with any motion or text object
Example
To copy the current line to the system clipboard:
"+yy
To copy a visual selection to the system clipboard:
- Select text with
v,V, or<C-v> - Press
"+y
The selected text is now on your system clipboard and can be pasted into a browser, terminal, or any other application with Cmd+V (macOS) or Ctrl+V (Windows/Linux).
Tips
- Use
"+pto paste from the system clipboard into Vim - Use
"*yto yank to the primary selection register (Linux X11 middle-click paste) — on macOS and Windows,*and+behave identically - Combine with any motion or text object:
"+yiwcopies the word under the cursor,"+yapcopies the current paragraph,"+y$copies to end of line - Your Vim must be compiled with
+clipboardsupport — check with:echo has('clipboard'). If it returns0, install a clipboard-enabled build (e.g.,vim-gtk3on Ubuntu, or use Neovim which includes clipboard support by default) - To always use the system clipboard for all yank and paste operations, add this to your vimrc:
set clipboard=unnamedplus
- With
clipboard=unnamedplus, everyyandpautomatically uses the system clipboard, so you no longer need the"+prefix