2

I want to use visual block mode (ctl-v) to select all the comments but none of the code:

a = 1             // short comment
b = 2             // this one is a longer comment
c = 3             // shortest

My selection is marked with X characters here. Notice that if I start at the top-left my selection is unsatisfactory:

a = 1             XXXXXXXXXXXXXXXX
b = 2             XXXXXXXXXXXXXXXX longer comment
c = 3             XXXXXXXXXXX

If I start middle-left I lose either the first or third line:

a = 1             // short comment
b = 2             XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
c = 3             XXXXXXXXXXX

And so on. I'm missing something here. What is the secret? I've tried :help and searched here and Google.

9

The secret is to press $ after you have expanded your block vertically:

<C-v>jj$

or to press $ before expanding your block vertically:

<C-v>$jj

Well, $ is the secret.

…which is not that surprising, after all.

6

One (*) secret is:

set virtualedit=block

... in your vimrc :)

(:h virtualedit will explain everything).

A disadvantage of this method: when yanking, you get trailing spaces on the shorter lines.

(*) The other is in the answer provided by @romainl.


BTW, for the secret of the golden rivet, see this instead.

  • I've ticked @romainl but your answer was interesting too, and the clip :) – user251764 Aug 19 '15 at 9:13
  • You chose well :) – VanLaser Aug 19 '15 at 10:41

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.