Vim Paste From Clipboard

From
  1. Vim Paste From System Clipboard
  2. Vim Paste From Clipboard Mac

Vim can copy or paste to the X clipboard or the desktop clipboard. This is useful to copy text from Vim to an external application or vice versa. This is also useful to copy and paste between multiple instances of Vim running in different terminals.

If you paste from this register in Vim, then the text in the clipboard appears at the paste location. To copy to system clipboard, visually mark the text and type: '+y. To paste from system clipboard: '+p. Note that Vim may be copying everything you visually mark to clipboard by default. Vim also provides a pasting register for you to paste text from the system clipboard. You can use '.p or '+p depending on your system. On a system without X11, such as OSX or Windows, you have to use the. register. On an X11 system, like Linux, you can use both.

This capability is available in Vim only if it has been compiled with the +clipboard feature. With this feature, the doublequote-plus register '+ of Vim is connected to the desktop clipboard. Anything you cut, copy or yank to this register appears in the system clipboard. If you paste from this register in Vim, then the text in the clipboard appears at the paste location.

  • To copy to system clipboard, visually mark the text and type: '+y

  • To paste from system clipboard: '+p

  • Note that Vim may be copying everything you visually mark to clipboard by default. This is because most Vim builds have the autoselect feature turned on. This autoselect feature can be disabled if you wish to.

  • This feature just does not work on some systems for me. I have resorted to using xclip to copy and paste between Vim and desktop clipboard.

Tried with: Vim 7.4 and Ubuntu 16.04

Vim

Many systems ship with a version of Vim that was compiled with the -clipboard feature disabled, which is a damned nuisance! Being able to access the system clipboard from Vim is an essential feature. Let’s look at a few ways of getting the +clipboard feature on OS X and Ubuntu.

On OS X

On OS X Mavericks, Apple ships Vim version 7.3 with -clipboard. Here’s the gist from running /usr/bin/vim --version on Mavericks (and the same on Mountain Lion). Shame on you Apple!

If you use Homebrew, you can get Vim with +clipboard by running:

Here’s a gist from running /usr/local/bin/vim --version.

Alternatively, you could download MacVim. Look inside /Applications/MacVim.app/Contents/MacOS, and you’ll see that the app provides two binaries: MacVim, which launches the GUI, and Vim, which runs in the Terminal with the same feature set. Both versions include the +clipboard feature. Here’s a gist from running /Applications/MacVim.app/Contents/MacOS/Vim --version.

On Linux

Vim Paste From System Clipboard

On desktop Linux systems the clipboard is handled by the X window system. Most systems that use X11 will ship a version of Vim with the +clipboard feature. If your desktop Linux distribution ships with Vim without the +clipboard feature, you should be able to install a Vim package that provides this feature, e.g. each of these packages for Ubuntu provides Vim with +clipboard: vim-gnome, vim-athena, and vim-gtx.

Robin Skahjem-Eriksen wrote to me with a tip: you can run GVim inside the terminal by launching it with the gvim -v command. That could be handy if your distribution ship vim with -clipboard, but also ships gvim with +clipboard.

How

When Linux runs on a server it doesn’t usually include X11. In this environment, it makes sense for Vim to be built with the -clipboard feature disabled. Some Vim packages are intended for systems without X11, such as the vim-nox package. It’s possible to install packages on a Linux server that would add the +clipboard feature for Vim, but doing so would also install X11 and all its dependencies. That’s probably not a good idea.

Can we fix this?

Being able to access the system clipboard from Vim is essential. It’s a nuisance that some desktop systems ship Vim without the +clipboard feature! I’d like to see that change. Please make the information in this article obsolete by campaigning to have +clipboard enabled by default on your system.

Update: I’ve revised this article, because my understanding of Linux was terribly flawed. You can find the original draft here. Thanks to Will Gray for his patient explanations.

Vim Paste From Clipboard Mac

Comments