3

My vim is not compiled with clipboard feature. So, i use xsel as suggested by Gilles to copy/paste the contents.

I use :.w !xsel -b for selecting the current line and, :w !xsel -b to select the entire document.

But sometimes, i need to move multiline selection done using V to clipboard. How can i do using xsel?

Note: i tried Ctrl-Shift-c, it didn't work. Anyways, i am not a great fan of Ctrl combo, since it breaks my fingers.

4

You can still do basically the same thing from visual mode. Once you have the area selected, hit the following1:

:'>,'> !tee >(xsel -b)

You need to pipe to tee so that the contents of your selected area isn't cleared.

1Adapted from here.

  • 1
    thanks a ton, turner.. it worked like charm... only thing is, when i am in visual mode and press :, it enters :'<,'> and not :'>,'> as given here.... – Madhavan Jul 29 '15 at 15:41
  • Whoops, let me correct that - you're right – mjturner Jul 29 '15 at 15:42
  • no, turner... i changed the arrows direction manually as per your answer to make it work... it says, the arrows indicate some ranging stuff.. not sure what it does... only the old answer of yours work... – Madhavan Jul 29 '15 at 15:46
  • Sorry - reset it to my original answer – mjturner Jul 29 '15 at 16:32
3

Type :w !xsel -b to copy the visual selection. When you press : while the visual selection is active, this inserts a range that designates the visual selection, so you'll end up executing :'<,'>w !xsel -b.

This copies all lines that are partially or completely selected, i.e. line where the selection starts and the line where the selection ends are fully included, even the parts that weren't selected. I don't know how to copy the selection exactly.

Of course, if Vim is compiled with the +clipboard option, just use "+y.

I've looked for a way to make "+ use xsel when Vim doesn't have clipboard support, but I didn't get a fully working answer, this seems to be very difficult.

2

Turns out this is infuriatingly easy: use the "v" command to highlight the text area you want. Immediately press the "!" command, followed by your command.

So the sequence is "v" "!"

  • Important notice: selected text will be replaced with output of the command you enter. If you use xsel without arguments then the text will be copied to clipboard and returned back, so the text in editor won't be changed. But xsel -i won't output the text back. Be careful. – MarSoft Jun 16 '17 at 22:50

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.