Is there a way to pipe selected text through an external command in vim, but not to replace the selected text with the output? Occasionally I want to copy something in a vim buffer to the X clipboard, so I use something like %!xclip -- but then I have to "undo" that command to get the text back since xclip doesn't return anything. Short of writing a wrapper script around xclip that also echoes the text back, is there a better way to do this?


  • If you use gvim you can copy directly to the X clipboard with the special register +. "+yy copies current line to clipboard. You can also use visual selections. – Keith Jun 10 '11 at 12:07

Instead of %!xclip use

%w !xclip

Note the space between the w and the !. See

:help :w_c

For copying to the clipboard, I use


For more on the difference between between "* and "+, you'll want to check out :h x11-selection. For me, the following seems to be relevant:

Xterm, by default, always writes visible selections to both PRIMARY and CUT_BUFFER0. When it pastes, it uses PRIMARY if this is available, or else falls back upon CUT_BUFFER0. For this reason, when cutting and pasting between Vim and an xterm, you should use the "* register. Xterm doesn't use CLIPBOARD, thus the "+ doesn't work with xterm.

So I'll be sticking with "* instead of "+ that peth mentioned, but you might do well to test which ones works best for your usage pattern.

  • 1
    Yup, * register is closer to question as xclip uses the PRIMARY selection by default, too. – peth Jun 10 '11 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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