获取不同行的文本,在 Vim 中使用 ex

发布于 2024-09-07 19:11:42 字数 498 浏览 1 评论 0原文

假设我在 Vim 中有以下文本:

file1.txt
file2.txt
file3.txt

renamed1.txt
renamed2.txt
renamed3.txt

我想要如下转换:

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

我的想法类似于以下内容:

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

我一直不知道如何指定 部分。

我尝试过类似 .+4 (当前行下方 4 行)但无济于事。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

软甜啾 2024-09-14 19:11:42

您可以使用分块切割和分块切割来做到这一点。粘贴。

1) 在每个“重命名”行的开头插入空格,例如 :5,7s/^/ /

2) 使用分块视觉选择 (ctrl-v) 进行选择所有“file”行,然后按 d 删除它们

3) 再次使用块视觉选择来选择所有重命名行开头的空格字符,然后按 p >。这会将您删除的块中的相应行粘贴到每行的开头。

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.

温柔戏命师 2024-09-14 19:11:42
:1,3:s/\ze\n\%(.*\n\)\{3}\(.*\)/ \1

解释:

 \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

这会将后面的行保留在原来的位置。

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

酷遇一生 2024-09-14 19:11:42

我真的会为此制作一个宏。删除下面的行,向上移动,粘贴,加入行,然后在其他行上运行宏。我认为合适的另一种方法是一个单独的脚本来充当过滤器。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文