13

assume I have following texts:

aaa-dd-e: "aaa",
bb-x: "bb",
c-9: "c"

I want to do selection like below:

aaa-dd-e: "aaa",
bb-x: "bb",
c-9: "c"

I tried using c-v to create a visual block, but after I create selection on the first column and I press "f:"( I meant to expand the selection to colon mark on every line like above), what I get is:

aaa-dd-e: "aaa",
bb-x: "bb",
c-9: "c"

the selection stops at the first colon and creates a rectangle.

2

4 Answers 4

7

Selection is generally a mean to an end, more so in Vim where selection is often not necessary in the first place.

Assuming you still want to put quotes around property names in your non-JSON file to turn it into proper JSON:

  1. select the lines to modify, character-wise or line-wise, it doesn't matter,
  2. :'<,'>norm I"^[f:i" ('<,'> is automatically inserted for you and ^[ is obtained with <C-v><Esc>).

If you have the beautiful surround plugin, step 2 becomes:

:'<,'>norm ^vt:S"

With a substitution, step 2 could become:

:'<,'>s/^\s*\([^:]*\):/"\1"
3
  • you guys are right, I now find that visual selection probably unnecessary. with the help or :g/regex/norm, I can simply achieve whatever I want to do.
    – Aaron Shen
    Sep 1, 2017 at 1:29
  • Is there any way that I can put the whole selected block to system clipboard and paste in another file?
    – Aaron Shen
    Sep 1, 2017 at 1:31
  • Of course. Assuming your Vim is compiled with clipboard support you can do "+y or "*y, depending on which clipboard you want to target. There are many related questions around here.
    – romainl
    Sep 1, 2017 at 5:29
15

Vim only supports rectangular blockwise selections, with the exception of the jagged right border when you press $.

There are plugins (like vim multiple cursors) that can emulate multiple selections, but you should also be able to achieve your actual goal (which you unfortunately didn't mention) with pure Vim commands, for example :global and :substitute (if you can formulate a regular expression that matches the selection in each line), or by repeating macros (if there's a particular motion that covers the selection in each line).

2
  • 2
    what if I want to cut those lines and paste into another buffer?
    – Aaron Shen
    Aug 31, 2017 at 13:46
  • Ah, man. That $ is what I've needed.
    – paradroid
    Feb 1 at 5:44
1

Just to throw another hat into the ring, if you really want to use visual selection you can use the Tabular plugin to line up the colons, then make your visual selection.

If you take your original text:

aaa-dd-e: "aaa",
bb-x: "bb",
c-9: "c"

:Tab /:\zs will align everything after the colon, as below

aaa-dd-e:  "aaa",
bb-x:      "bb",
c-9:       "c"

And then you can make your visual block selection.

As others have said, though, visual selection is probably not necessary in this situation. Using norm or substitution can probably accomplish what you want without fussing with visual selections.

1

I've found a solution to this issue which is surprisingly simple! Start visual block mode from the bottom of the selection of the text you want to copy and select upwards! It will expand the block as you go up, whereas if you highlight downwards it appears to make the selection smaller.

2
  • But this way it will always be a rectangular block, right? As I read it, this isn't what the OP wants (they mention they tried visual block mode). Jun 14, 2018 at 19:06
  • No. It will always be a rectangular block if all lines are the same length. I've found that starting at the bottom and highlighting upwards and then pressing $ to go to the end of the line will result in all highlighted lines highlighted to the end of the line. Jun 18, 2018 at 8:04

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.