13

I work a lot with visual block mode, which I find really useful for quick changes across a lot of lines.

But something I'm struggling to do is inserting a newline like so:

Selection is noted as []:

Some [t]ext
Some [t]ext
Some [t]ext
Some [t]ext
Some [t]ext

I press I and I can add some text to be inserted before the t:

Some other text
Some [t]ext
Some [t]ext
Some [t]ext
Some [t]ext

When I press <Esc> I get the following:

Some other text
Some other text
Some other text
Some other text
Some other text

But now if I press <CR> instead of the text I want to add, the newline is inserted as desired but not for the other lines. The result is the following:

Some 
text
Some text
Some text
Some text
Some text

Do you know why this isn't working?

Note: since it works for c and r I'm not sure why is this not working...

5
  • If the intention is to create new line after some character for selected lines, we can try a macro. Go to nth column and enter a new line and repeat it till the selection – SibiCoder May 24 '16 at 10:15
  • Yes, or a substitution could also work, but my intention is to work out the way to do within visual block mode. – nobe4 May 24 '16 at 10:17
  • 5
    This can't be done in visual block mode, unfortunately. Use a substitution or a recording instead. – romainl May 24 '16 at 15:30
  • @romainl Why can't this be done in visual mode? It would be great if you can point out the reason (I would appreciate it If you can give hints or references as to why newline is treated differently). – Cyriac Antony Jan 13 at 4:51
  • 1
    @CyriacAntony, I and A in visual block mode just don't allow that. I think this is implied by "Works only for adding text to a line, not for deletions." under :help v_b_i because that newline a) removed text from the line so it's akin to a "deletion" and b) it changed the number of lines in the selection. But it probably deserves more explanation. – romainl Jan 13 at 9:06
5

Visual block I doesn't work like that. The best you can probably do is something like the following.

Highlight a column in visual block mode (with [b] denoting a highlighted character as in your question):

aaa[b]bb
aaa[b]bb
aaa[b]bb
aaa[b]bb
cccccc

Perform a substitution on the visual area with \%V:

:'<,'>s/\%V/\r/

Vim will insert the '<,'> for you when you press :, so you don't need to type that bit.

And the result:

aaa
bbb
aaa
bbb
aaa
bbb
aaa
bbb
cccccc

If you need to split lines at a visual block often, chances are it'll be readily available in your history, and you may not need to do any more than highlight, then :<up><return>.

1
  • Nice use of the \%V even if it's not what I wanted I'll accept this (but what I want seems impossible anyway ^^) – nobe4 Jun 3 '16 at 15:03

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.