keepcalculator

06 Numbers and Powers

Enable Clipboard in Vim: A Complete Guide

Card No. 06·87074 · Updated · Standard formula, shown below

To enable clipboard support in Vim, you need a version compiled with the +clipboard feature and set the clipboard option to unnamedplus. This lets you yank and paste using the system clipboard directly from Vim, without extra commands.

What Does Clipboard Support Mean in Vim?

Vim has its own internal registers for storing yanked or deleted text. By default, these registers are separate from your operating system’s clipboard. When you copy text in Vim and paste it into another application (or vice versa), nothing is shared unless you explicitly use a special register like "* or "+. Enabling clipboard support makes Vim use the system clipboard as the default register, so yanking, deleting, and pasting behave more like a standard text editor.

Check If Your Vim Has Clipboard Support

Before changing any settings, verify whether your Vim build includes the +clipboard feature. Run this command in your terminal:

vim --version | grep clipboard

Look for +clipboard (with a plus sign) or -clipboard (with a minus sign). If you see +clipboard, you’re good to go. If you see -clipboard, your Vim lacks the feature and you’ll need to install a different version (see below).

How to Enable the System Clipboard

Once you confirm +clipboard is present, add this line to your ~/.vimrc file:

set clipboard=unnamedplus

This makes the unnamed register (the one used by default for yank and delete) point to the system clipboard. On Linux, this uses the + register, which corresponds to the X11 primary and clipboard selections. On macOS and Windows, it works with the standard clipboard.

If you prefer to keep the default behavior but still want easy access to the system clipboard, you can use the "+ register manually:

  • Yank to system clipboard: "+y
  • Paste from system clipboard: "+p

Installing a Vim with Clipboard Support

If your Vim shows -clipboard, you need a different build. Here are common options:

  • Linux (Debian/Ubuntu): Install vim-gtk3 or vim-gnome which include clipboard support: sudo apt install vim-gtk3
  • macOS: The default Vim that ships with macOS often lacks clipboard support. Install a newer version via Homebrew: brew install vim
  • Windows: The official Vim installer from vim.org includes clipboard support by default.

After installing, verify with vim --version | grep clipboard again.

Common Problems and Solutions

Even with +clipboard and unnamedplus, you might encounter issues:

  • Pasting doesn’t work in insert mode: Use Ctrl+Shift+V (or Cmd+V on macOS) to paste from the system clipboard into insert mode. Alternatively, use "+p in normal mode.
  • Yanking from Vim doesn’t appear in other apps: Make sure you’re using the correct register. With unnamedplus, normal y should work. If not, try "+y.
  • Clipboard is cleared when Vim exits: This is normal on some systems. Use a terminal multiplexer like tmux or a GUI Vim (gvim) to keep clipboard content persistent.
  • Vim in tmux doesn’t share clipboard: You may need to configure tmux to set the clipboard. Install xclip or xsel on Linux and add set -g set-clipboard on to your tmux config.

Clipboard Registers Explained

Vim has two special registers for the system clipboard:

  • "* — The primary selection on X11 (middle-click paste). On Windows and macOS, this is the same as the standard clipboard.
  • "+ — The clipboard selection on X11 (Ctrl+V / Cmd+V). On Windows and macOS, this is the same as the standard clipboard.

Setting clipboard=unnamedplus makes the unnamed register (used by y, d, p) alias to "+. If you only want to alias to "*, use set clipboard=unnamed instead.

Alternative: Using a GUI Vim

If you prefer a graphical interface, gVim (Vim’s GUI version) has native clipboard support and often behaves more like a standard editor. You can still use the same set clipboard=unnamedplus in your .vimrc for consistency. gVim also provides menus for copy and paste, which can be easier for beginners.

Frequently Asked Questions

Why doesn’t my Vim have clipboard support?

Many Linux distributions and macOS ship a minimal Vim without the +clipboard feature to keep the binary small. Installing a version from a package manager or Homebrew usually resolves this.

What’s the difference between unnamed and unnamedplus?

unnamed uses the * register (primary selection on Linux), while unnamedplus uses the + register (standard clipboard). On Windows and macOS, both are the same, so unnamedplus is recommended.

How do I copy text from Vim to a browser?

With unnamedplus, simply yank in Vim (y) and paste in the browser with Ctrl+V (or Cmd+V). If it doesn’t work, check your Vim version and clipboard option.

Does Neovim have clipboard support?

Neovim has built-in clipboard support and uses the same clipboard option. It often works out of the box, but you may need to install xclip or wl-clipboard on Linux for Wayland.

Can I enable clipboard support only for certain files?

Yes, you can use an autocommand to set the clipboard option based on file type. For example, add this to your .vimrc to enable it only for Python files:

autocmd FileType python set clipboard=unnamedplus

Why does pasting in Vim indent my code incorrectly?

This is a common issue with auto-indentation. Use :set paste before pasting to disable auto-indent, then :set nopaste after. Or use "+p in normal mode, which often avoids the problem.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →