tags:

views:

71

answers:

3

Let's say I have the following text in Vim:

file1.txt
file2.txt
file3.txt

renamed1.txt
renamed2.txt
renamed3.txt

I want a transformation as follows:

file1.txt renamed1.txt
file2.txt renamed2.txt
file3.txt renamed3.txt

What I have in mind is something like the following:

:1,3 s/$/ <the text that is 4 lines below this line>

I'm stuck with how to specify the <the text that is 4 lines below this line> part.

I have tried something like .+4 (4 lines below the current line) but to no avail.

+3  A: 

I would make a macro for this really. Delete the lower line, move up, paste, Join lines, then run the macro on the others. The other method I think would be appropriate is a separate script to act as a filter.

Daenyth
+6  A: 

You can do it with blockwise cut & paste.

1) insert space at the start of each "renamed" line, e.g. :5,7s/^/ /

2) Use blockwise visual selection (ctrl-v) to select all the "file" lines, and press d to delete them

3) use blockwise visual selection again to select the space character at the start of all the renamed lines, and press p. This will paste the corresponding line from the block you deleted to the start of each line.

Dave Kirby
Wow, I didn't know you could do that! Awesome!
Daenyth
I think you mean `P` not `p` because you will want to paste before the space, not after it.
Andreas Grech
@Andreas: Try it - p works as I described. Using P puts two spaces between each fileN.txt and renamedN.txt, but I am not sure why. Block selecting seems to add a space onto the end of each line, even if there are no spaces at the end of the lines selected. In either case, the paste replaces the currently selected block, which is why you need to add a space to the start of each line.
Dave Kirby
I tried it, that's why I commented. With a `p`, this is how the line becomes: `_file1.txtrenamed1.txt` (the `_` is a space); but with a `P`, it's correct: `file1.txt renamed1.txt`. Thing is, `p` pastes after the cursor and `P` pastes before the cursor and you need to paste before the space of the renamed files.
Andreas Grech
+3  A: 
:1,3:s/\ze\n\%(.*\n\)\{3}\(.*\)/ \1

explained:

 \ze - end of replaced part of match - the string matched by the rest of the pattern will not be consumed
 \n - end of current line
 \%(.*\n\)\{3} - next 3 lines
 \(.*\) - content of 4th line from here

This will leave the later lines where they are.

rampion