1

I may have seen this question before on Vi Exchange but I'm having trouble locating it while searching now. If it's already been answered, please let me know the duplicate and I can find that.

I have text like this:

enter image description here

And I would like to copy that text to the clipboard. The command I'm using is:

:'<,'>w !it2copy

However, it seems to do a line-wise copy (as if I am in Visual-Line mode), as the clipboard now shows:

my_string = """SELECT * FROM
mytable WHERE id=4"""

Instead of, what I want:

SELECT * FROM
mytable WHERE id=4

How can I get the exact cursor selection instead of the line-wise equivalent?

Note that an answer should take the copy-paste across multiple-lines into account.

9
  • 1
  • If you don't want to filter the text, pass the n flag to :s so that it doesn't substitute the selection. – user938271 Aug 31 '20 at 21:35
  • 1
    How about yanking the visual selection in the unnamed register, then writing it into the clipboard with call system('it2copy', @")? – user938271 Aug 31 '20 at 21:45
  • 1
    Not sure why you can't yank the text in the unnamed register, then write it on the stdin of it2clip via system(), but, fwiw, I have a mapping which leverages the sequence OSC 52 for this kind of situations. It relies on this script. – user938271 Aug 31 '20 at 21:55
  • 1
    @user938271 holy cow, a 10k line vim settings file?! – David542 Sep 1 '20 at 3:27
2

To apply Ex command to an arbitrary selection you can do the following:

  1. Copy selection to the end of file
  2. Select those new lines and run the command
  3. Cut the result
  4. Replace old selection with the result

Of course, it's no good in doing this manually, so the plugins exist.

I know of VIS (written by DrChip), and vim-opera (that one is mine; it also provides an easy-to-use interface for g@ operator). Probably, there must be others too.

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.