Search code examples
regexvimex

Getting text that is on a different line, with ex in Vim


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.


Solution

  • 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.