Clipboard on X11, Alacritty, and Vim

(This is part of a larger series on finding your footing on Arch Linux.)

Last modified: 29 May 2022

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:

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:

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):

Alacritty

For users with default key bindings…

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.)

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:

Vim clipboard theory

Suggested prerequisite knowledge:

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

  1. Set clipboard=unnamedplus to make Vim use the * register (and thus the CLIPBOARD selection) for all yank, delete, change and put operations.

  2. Set clipboard=unnamed to make Vim use the * register (and thus the PRIMARY selection) for all yank, delete, change and put operations.

  3. Set clipboard=unnamed,unnamedplus to 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.

  1. Technically there are three X clipboard-like buffers—CLIPBOARD, PRIMARY, and SECONDARY, but the SECONDARY buffer is rarely used.