Vim Clipboard Skills

October 2, 2025

Abstract

Working with Vim's clipboard functionality can be tricky, especially when you need to copy text between Vim and external applications. This comprehensive guide explains how to seamlessly integrate Vim with your system clipboard, manage registers effectively, and handle clipboard operations in different environments including virtual machines.

Keywords: Vim, Text Editor, Clipboard, Development Tools, Productivity

Copying from Vim to the System Clipboard

The key to copying text from Vim to the system clipboard (and vice versa) is ensuring that your Vim installation supports clipboard access and is properly configured.

Prerequisites: Checking Clipboard Support

First, verify that your Vim installation includes clipboard support by running:

vim --version | grep clipboard

If you see +clipboard and +xterm_clipboard, your Vim supports system clipboard integration. If you see -clipboard instead, you'll need to install a Vim version with clipboard support, such as vim-gtk3, vim-gnome, or gvim.

Configuring Vim for System Clipboard

Add the following line to your ~/.vimrc file:

set clipboard=unnamedplus

This configuration makes Vim's default yank and paste operations use the system clipboard, allowing you to use standard y (yank) and p (paste) commands to interact with the system clipboard.

Basic Clipboard Operations

Copying to the System Clipboard

In visual mode, select the text you want to copy and press:

  • "*y - Copy to the PRIMARY selection (X11)
  • "+y - Copy to the CLIPBOARD (system-wide)

Pasting from the System Clipboard

Position your cursor and press:

  • "*p - Paste from PRIMARY selection
  • "+p - Paste from CLIPBOARD

Selecting and Copying All Content

The command ggVGy (select all and copy) only copies to Vim's default register, not the system clipboard. To copy all content to the system clipboard, use:

ggVG"+y

Or alternatively:

:%y+

These commands copy content to the system clipboard register (requires +clipboard support).

Important Note: This works effectively on macOS with terminal-installed Vim, but may not work in Vim running inside virtual machines like Orbstack's Ubuntu without additional configuration.

Clipboard Operations in Virtual Machines

When working with Vim inside a virtual machine (such as Ubuntu in Orbstack), clipboard synchronization between the VM and host machine isn't automatic, even if Vim supports +clipboard.

Key Limitations

  1. Isolation by Default: Virtual machines (whether headless or with desktop environments) isolate their system clipboard from the host by default.

  2. Limited Scope: Vim's "+ register only interacts with the VM's internal X11 (or Wayland) clipboard, not directly with the host system's clipboard.

  3. Solution: Clipboard sharing between VM and host requires:

    • Installation of guest additions/tools (VirtualBox Guest Additions, VMware Tools, or QEMU Guest Agent)
    • Enabling shared clipboard functionality in VM settings
    • Proper configuration of the clipboard sharing feature

Terminal Paste Shortcuts

In most terminal emulators, you can paste clipboard content using:

  • Ctrl+Shift+V (Linux/macOS)
  • Right-click → Paste

Advanced Tips

Auto-save Clipboard on Vim Exit

For pure terminal environments, you can configure Vim to automatically save yanked content to the system clipboard when exiting. Install xsel and add this to your .vimrc:

autocmd VimLeave * call system("xsel -ib", getreg('+'))

This ensures that clipboard content persists after closing Vim.

Understanding Vim Registers

Beyond the "+ (system clipboard) register, Vim provides numerous registers for different purposes:

Default Registers

  • "" (Unnamed register): Stores the most recent yank or delete operation
  • "0 (Yank register): Stores the most recent yank operation specifically
  • "1 to "9 (Numbered registers): Store the last 9 delete operations
  • "- (Small delete register): Stores small deletions (less than one line)

Named Registers

  • "a to "z: User-defined registers (26 total) for manual storage and retrieval

Special Registers

  • "* (Selection register): Corresponds to X11's PRIMARY selection (mouse selection)
  • "+ (Clipboard register): Corresponds to X11's CLIPBOARD (system-wide clipboard)
  • "= (Expression register): Evaluates expressions and returns results
  • "_ (Black hole register): Discards any content written to it (useful for deleting without affecting other registers)

Read-only Registers

  • ": - Last executed command
  • ". - Last inserted text
  • "% - Current file name
  • "# - Alternate file name
  • "/ - Last search pattern

Viewing Register Contents

You can view all registers and their contents by running:

:reg

This command displays a comprehensive list of all registers and their current values, making it easy to track and manage your clipboard operations.