If I'm using a build of Vim with the xterm_clipboard feature then the system clipboard content is available through the + register.

If the build I'm using doesn't have this feature, can I make the "+ register call my custom programs to retrieve and set the system clipboard (xsel -bo and xsel -bi, or likewise with xclip)? Likewise with "* to call xsel -po and xsel -pi.

The point is to be able to type something like "+p without having to worry whether the feature is available. Typing :r !xsel -b<Enter> is not what I'm looking for: I want the same keystrokes to work, and I want the paste variants to follow what's available for “true” registers (P, gp, …).

Bonus: can I define custom commands for other register names?


1 Answer 1


A few <expr> mapping should be able to do the job here. The basic structure would be

function! ClipboardOrXclip(command, register)
    if a:register !~ '[+*]' || has('xterm_clipboard') || has('gui_running')
        " Just return the original command if the clipboard is accessible
        " or it's not a register that should be handled by xsel
        return a:command
    if a:register == '+'
        return "<Esc>:r !xsel -bo<CR>"
        return "<Esc>:r !xsel -po<CR>"

nnoremap <silent> <expr> p ClipboardOrXclip('p', v:register)

The function would need to be expanded to handle all the variations of p, P, gp, etc. but this should be a good start.

  • This seems to be on the right track, but it isn't working. A plain p (without a preceding " and register name) doesn't do anything other than make "p appears in the status line. "ap is equivalent to a, "bp beeps, … I don't get the logic. "+ beeps immediately, I don't even get to say that I mean + or * as a register name. Feb 4, 2015 at 18:33
  • 1
    Assuming these fundamental problems are solved, is there an exhaustive list of commands I'd need to override? The set looks daunting, especially if I want to support yanking as well. Feb 4, 2015 at 18:34
  • @Gilles Updated to fix the behavior with non-+/* registers. The problem with "+ when your vim has -xterm_clipboard is more difficult because it never gets to this function. Specifying an invalid register (+) aborts the command. SO, it looks like the behavior actually has to be inverted so map the "+ or "* and then handle the command specified by the user...
    – jamessan
    Feb 4, 2015 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.