Clipboard on X11, Alacritty, and Vim
(This is part of a larger series on finding your footing on Arch Linux.)
Goal: Comfortably copy and paste between GUI applications in the X Window System, the Alacritty terminal, and Vim/Neovim.
Dependencies: This guide works on the X Window System, and is targetted towards Alacritty and Vim/Neovim users. You should first set up X if you have not yet done so.
References:
- ArchWiki: Clipboard: lists tools for interacting with the clipboard
- Answer to StackExchange: How to toggle or turn off text selection being sent to the clipboard: how the
PRIMARYandCLIPBOARDselections work in X - Official freedesktop.org clipboard specification: quite informative once you get paste the boring plain-text formatting
An X clipboard crash course
(To help new users coming from Windows and macOS; feel free to skip.)
TLDR: X has two clipboards, called CLIPBOARD and PRIMARY. Use CLIPBOARD for Windows/macOS-style copy and paste and PRIMARY for text selected by the mouse.
End TLDR.
Windows and macOS have one system-wide clipboard.
The X Window System commonly used on Linux has two1 standardized system-wide buffers that act as independent clipboards.
Loosely, in macOS/Windows terms, you basically have two clipboards.
Their names are CLIPBOARD and PRIMARY, and here is what they do:
-
CLIPBOARD: essentially the equivalent of the macOS or Windows clipboard. Copy text intoCLIPBOARDwith GUI menu options or<Ctrl>-C(or in rare cases, e.g. Alacritty, a similar keyboard shortcut), and paste fromCLIPBOARDwith GUI menu option,<Ctrl>-V, or a similar shortcut. -
PRIMARY: used specifically to manipulate text selected with the mouse. Any text selected by the mouse in X applications (e.g. highlighted text in a web browser) is automatically stored inPRIMARY. You paste the contents ofPRIMARYwith a middle mouse click.
Copying text into CLIPBOARD requires an explicit action on the user’s part (e.g. button press, keyboard shortcut), while mouse-selected text is automatically copied into PRIMARY without explicit action on the user’s part.
Many users, especially those interested in a Windows/macOS-like experience, will probably use the CLIPBOARD buffer more than PRIMARY.
The rest of this article shows how to get text into and out of CLIPBOARD in various programs, allowing you to copy and paste between programs via the CLIPBOARD buffer.
GUI applications
In most X GUI applications (e.g. a web browser):
- Copy text into
CLIPBOARDwith<Ctrl>-Cor a “Copy” option in a GUI menu. - Paste text stored in
CLIPBOARDwith<Ctrl>-Vor “Paste” menu option.
Alacritty
(This section assumes haven’t changed Alacritty’s default copy/paste key bindings, in which case you probably already knew what you were doing.)
-
Copy text into
CLIPBOARDwith<Ctrl>-<Shift>-C(and not<Ctrl>-Clike most other X applications). Alacritty intentionally avoids<Ctrl>-Cfor copying because<Ctrl>-Cis nearly universally used to send the interrupt signalSIGINTto programs in the shell. -
Paste text stored in
CLIPBOARDwith<Ctrl>-<Shift>-V(and not<Ctrl>-V)
Custom Alacritty key bindings
You can change Alacritty’s default CLIPBOARD copy/paste keys in the key_bindings: section of the alacritty.yml config file—you’ll need to bind keys to Alacritty’s Paste and Copy actions.
Here are the default bindings to give you a feel for the syntax.
key_bindings:
- { key: V, mods: Control|Shift, action: Paste }
- { key: C, mods: Control|Shift, action: Copy }The # Key bindings section in the default alacritty.yml file contains all the documentation you need to define your own bindings.
(You can find the latest alacritty.yml file on the Alacritty GitHub release page.)
Bonus: Copying with Alacritty Vi mode
If you’re familiar with Vim keybindings, you can also copy text in Alacritty using Alacritty’s Vi mode. Here are the tools you need:
<Ctrl>-<Shift>-<Space>enters Vi mode (you can configure this key binding using theToggleViModeaction inalacritty.yml).- Navigate with standard Vim key bindings, e.g.
h,j,k,l,w,b, etc. venters visual mode, from which you select the text you want to copy (Vfor visual line mode is also supported).yin visual mode copies selected text toCLIPBOARD.<Ctrl>-<Shift>-<Space>exits Vi mode.
Vim and Neovim
Goal: Make Vim/Neovim’s yank, delete, and change operations copy into system CLIPBOARD, and make Neovim’s put (paste) operation paste from the CLIPBOARD.
Example use case: copy a URL in a web browser with <Ctrl>-C, then paste the URL into Neovim with the default p action.
Requirements
Note: Vim and Neovim have different clipboard interfaces. Here’s what a typical user needs to know:
-
Neovim users: Neovim communicates with the system clipboard via a clipboard provider program (see Neovim’s
:help clipboardfor more information). For our purposes, this means you should install a third-party clipboard provider; I suggestxclip, which you can install withsudo pacman -S xclipNeovim will notice
xclipis installed and take care of the rest. To double check, you can use:checkhealthin Neovim to test clipboard status; an example output ifxclipis correctly installed might look like this:## Clipboard (optional) - OK: Clipboard tool found: xclip -
Vim users: your version of Vim must be compiled with the
+X11and+clipboardfeatures to properly interact with the XCLIPBOARDandPRIMARYselections. You can check this by runningvim --versionon a command line; the output should show+X11and+clipboard. Ifvim --versionshows-X11or-clipboard, you need a new version of Vim. You could either compile from source with the desired features or use the following workaround:- Install gVim (a GUI-compatible version of Vim) with
sudo pacman -S gvim. - Remove
vimif prompted bypacmanabout conflicting packages. - Use the
vimcommand as before;vim --versionshould now show+X11and+clipboard.
Why this works: the
gvimpackage includes a terminal version of Vim in addition to the gVim GUI, and the terminalvimincludes GUI features that regular Vim does not have. - Install gVim (a GUI-compatible version of Vim) with
Vim clipboard theory
Suggested prerequisite knowledge:
- The difference between X’s
CLIPBOARDandPRIMARYselections (scroll up and read An X11 clipboard crash course for a refresher.) - What Vim registers are and how to use them—a sentence like “use
"ayiwto yank a word into thearegister” or “use"bpto paste the contents of thebregister” should make sense to you. If needed, I suggest taking a 20-minute detour and learning about registers; a good place to start might be Brian Storti’s Vim registers: The basics and beyond, then moving on to the official documentation in:help registers
Both Vim and Neovim use the * register to interact with PRIMARY and the + register to interact with CLIPBOARD.
This means you can use operations like "+p to paste the contents of CLIPBOARD selection into Vim or "* to copy Vim text into the PRIMARY selection.
(For documentation, see the Selection registers "* and "+ section in :help registers.)
Configure the clipboard
You can configure Vim/Neovim to use the * and/or + registers for copy and paste through the built-in clipboard option.
You have three choices—in your vimrc or init.vim…
-
Set
clipboard=unnamedplusto make Vim use the+register (and thus theCLIPBOARDselection) for all yank, delete, change and put operations. -
Set
clipboard=unnamedto make Vim use the*register (and thus thePRIMARYselection) for all yank, delete, change and put operations. -
Set
clipboard=unnamed,unnamedplusto make Vim’s yank, delete, and change operations copy into both+and*, and make the put operations paste from+.
That should be it—Vim’s native yank/delete/change/put operations should now interact with the X CLIPBOARD and PRIMARY selections.
For documentation of unnamed and unnamedplus see :help 'clipboard' (make sure to include the single quotes!).
For more Vim-related copy/paste documentation than a typical user would ever want to read, check out :help 'clipboard', :help registers, :help quoteplus, and :help quotestar.
Reminder: Neovim users will need a clipboard provider (e.g. xclip) and Vim users will need a Vim with the +X11 and +clipboard features.
Scroll back up to the Requirements section for a refresher.
-
Technically there are three X clipboard-like buffers—
CLIPBOARD,PRIMARY, andSECONDARY, but theSECONDARYbuffer is rarely used. ↩