How do I access the X11 primary selection (middle-click buffer) separately from the clipboard in Vim on Linux?
Answer
"*
Explanation
On X11 Linux systems, there are two independent clipboard-like buffers: the primary selection ("*) and the clipboard ("+). The distinction matters when copying between Vim and other applications. Vim exposes each as a separate register, so you can explicitly read from or write to either one.
How it works
"*— primary selection: automatically set whenever you highlight text with the mouse in any X11 application, and pasted by middle-clicking. You don't have to press Ctrl+C — just selecting text is enough."+— clipboard: populated by explicit copy actions (Ctrl+C in most apps). Pasted with Ctrl+V or Shift+Insert.
Both registers support the standard Vim yank, delete, and put operators:
"*y{motion} " yank to primary selection
"*p " paste from primary selection
"+y{motion} " yank to clipboard
"+p " paste from clipboard
Example
To grab text that another application has highlighted (primary selection) without requiring that app to do a Ctrl+C:
"*p
To copy a Vim visual selection so another app's Ctrl+V will receive it:
V"*y
Tips
- On macOS and Windows,
"*and"+are aliases for the same system clipboard — the distinction only matters on X11 - Check if Vim was compiled with X11 clipboard support:
:echo has('x11')(returns 1 if available) :set clipboard=unnamedmakes"*the default register for all yank and paste operations:set clipboard=unnamedplusdoes the same for"+- Neovim requires
xclip,xsel, or a similar provider to be installed for these registers to work