r/vim•
humbled

Visual block insert/append line breaks?

This is failing for me and I expected it to work. Let's say I have a file full of data like this:

absolute_length_data:variable_length_data

Many, many lines of it. My mission: create line pairs of absolute length data and variable length data. It's not a clear-cut separator character; imagine that the colon separator can appear 0 or n times in the absolute length data, and it can also appear 0 or n times in the variable length data.

I had hoped that I could Ctrl-v and then find the absolute length separator, move down to block highlight all the lines I want to separate, and then A<CR><Esc> to break the lines up. But it only seems to insert on the first line of the visual block (tested with Vim up to 7.4.52). If I did it as a literal (A<C-v><CR>) vim shows the ^M in the line instead of making a linebreak, even though I can :%s/foo/<C-v><CR>/ and that replaces foo with linebreaks.

I ultimately accomplished my goal by doing a macro with position counting, but I still am curious as to why the visual block append didn't work, or how I could make that strategy work.

Posted Oct 14, 2013, 4:26 PM UTC

95
nandryshak•

I assume you know the column number, then? Simply do this:

%s/\%##c/\r/

where ## is the column number.

6
humbledOP•

That's cool. I always forget that you can do a lot of magic with vim regex.

1
batkarma•

Heh, that's interesting behavior. Same thing for v r <CR>

2
humbledOP•

Yeah, the documentation only calls out visual block change as not accepting line breaks.

1
gfixler•

I believe this is due to the way in which blockwise works. I don't think it has a buffer for line changes, the way global does. That's why g: does allow for things like this, because as you change lines, it adjusts the line numbers in its buffer to keep up. This is why g: and v: (its inverse) work well for filtering lines. They each go through recording changes first, then apply them in the end, sort of like how you need to tally changes in Conway's Game of Life first, then go through and flip cell states. I think the selection modes just literally work on lines, dumbly, and when some programmer realized that allowing newlines in screwed that up, they probably said "Okay, then, no newlines," and promptly hacked in something to stop you from adding them. That's my guess.

2