Pergunta

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.

Foi útil?

Solução

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.

Outras dicas

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

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top