14

Not easy to find a nice title for my problem.

I'm aware of inserting in front of a text-block:

Ctrl-v select lines I type text ESC

Now I would like to do this but also with replacing a part in my block selection. Currently I'm doing two operations

Ctrl-v select block x go back to start

Followed by the sequence I mentioned above.

Is there a way to combine these two operations by selecting (Ctrl-v) the block only once?

Here is a small example:

I would like to transform the following block

1  text
2  text
3  text
4  text
5  text

into

new text 
new text 
new text 
new text 
new text

doing only one "operation".

  • 1
    I'm not 100% sure what you want, maybe a real example would help (what you have vs. what you want). Are you aware of gv? It reselects the last visual selection and might be of help here. – PhilippFrank Mar 27 '15 at 9:02
21

Typing c after selecting your block instead of x will remove the text in the selection block and will enter insert mode. Once you type something and exit insert mode you should have the same change on every line.

Try

Ctrl-v -- select block -- c -- insert whatever -- Esc

|improve this answer|||||
1
    1
   1 1
  1   1
 1111111
 1     1
 1     1
 1     1

Picture a row of three different block letters like the one above. ^V selecting the above, I couldn't change all the ones to A in the last letter in the row due to unequal length lines. The previous block letters worked fine with :s/\%V1/T/g "Previous Block letter was a T made from '1's. Change /T/ to whatever.

But selecting the last block letter shown above it only changed all the '1's to 'A's to the point of the A*, and the rest remained ones.

*The shortest line.

To make all the lines the same length I padded them with spaces. (I am new to vim, so I am sure a better script could be written.)

Note: The peak of the A started on line 2.

let Maxlen = 0
" Find longest line
for i in range(2,8)
    let t = getline(i)
    let a = strlen(t)
    if a > Maxlen
        Maxlen = a
    endif
endfor

" Append spaces 'til all lines are the same length.
for i in range(2,8)
    let t = getline(i)
    let a = strlen(t)
    if a < Maxlen
        let b = Maxlen - a
        execute "normal!" . i . "gg0" . b . "A "
    endif
endfor

After this, :s/\%V1/T/g worked as advertised on the last block letter in the row.

P.S. I read that the execute line above should work without the dots, but it didn't work for me.

|improve this answer|||||
  • The execute line, goto the start of row i of the block letter and append b spaces to the end of the line. 'b' is the difference in length between the current line and the longest line. – Ron Feb 24 at 14:38

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.