Using Vim with the System Clipboard on Linux

3446

Vim mastery is not something that you acquire all at once. In fact, it’s more something that you slowly sneak up on. Bits at a time, you learn something new about Vim you didn’t know it could do. Or you’ll figure out an easier way to do something. Such is the case with using the system clipboard on Linux, at least for me.

Vim LogoIt’s a feature that I had ignored and worked around (badly) for some time. While I was perfectly comfortable yanking and pasting text within Vim, I hadn’t bothered to look into working with the system clipboard.

Getting Started

First off, you’re going to want to make sure you have the right packages installed to support Vim’s using the X clipboard. Make sure you have the regular Vim packages (and not just vim-tiny) and you’ll also want the xclip package. For some reason that I haven’t explored, Vim on Ubuntu/Linux Mint (and possibly other distros) doesn’t support the native feature for the system clipboard. So here I’m going to show you how to do it the right way (according to Vim’s docs) and the way that works on Linux Mint.

Got ’em? Good, let’s start with the official way first.

The Right Way

According to Vim’s documentation, you work with the system clipboard using the "* register. This is a special register that holds the system clipboard’s information.

So if you want to paste what’s in the system clipboard you’d use "*p in command mode. (So you’d use Esc first if you’re in insert mode.)

Want to copy something to the system clipboard? Use "*y or "*dd if you want to delete the line, etc.

The only problem? For some reason, I’ve found that "*p doesn’t always work on Ubuntu/Linux Mint. It does sometimes, but I’ve had problems with it. Try that method first, but if it doesn’t work (or if you want a slightly more familiar keybinding) read on.

The Way That Works

If the proper method fails, you’ll want to try this one out. The advantage here is that you can also set the copy and paste keybindings to something a bit more familiar. Well, familiar if you’re used to the copy keybinding from other applications.

Open up your .vimrc and paste these lines:

vmap <C-c> :<Esc>`>a<CR><Esc>mx`<i<CR><Esc>my'xk$v'y!xclip -selection c<CR>u
map <Insert> :set paste<CR>i<CR><CR><Esc>k:.!xclip -o<CR>JxkJx:set nopaste<CR>

To copy, you’ll use Ctrl-c. To paste, you’ll use Insert. Why not Ctrl-v for paste, since it’s the “paste” keybinding for most applications? It’s already claimed for starting Vim’s block selection.

One thing to note no matter which method you use: You don’t get Vim’s nifty block selection when pasting to another application. If you select a block of text (that is, columns of text) using Ctrl-v, you’ll just wind up pasting them as a line of text in another app.

However, you can use the system clipboard to paste between instances of Vim and that will carry over. So use Ctrl-v to copy in one terminal running Vim and "*p to paste, and you’ll get the block of text.

Like I said – Vim mastery happens a bit at a time. For some Vim users, this is likely to be old hat. For others, this may be a major revelation. OK, maybe a minor revelation, but useful nonetheless.