连接不匹配的行



我正在尝试加入所有没有匹配的行

示例文本:

text Like   
This is text of Line2  
This is text of Line3  
This is text of line4  
Like text  
This is text of line6  
This is text of line7  
Like  
This is text of line9  

所有不存在"Like"的行都必须加入(它们之间有空格(

最终结果:

text Like   
This is text of Line2 This is text of Line3 This is text of line4  
Like text  
This is text of line6 This is text of line7  
Like  
This is text of line9  

有人能帮我吗?

首先去掉尾部空格:

:%s/s+$

现在有一个加入这些行列的想法:

:v/Like/normal VnkJ

这应该是不言自明的。在不包含"赞"的每一行上,进入视觉逐行模式,搜索下一个"赞"行(它重复使用上一个模式(,向上一个并加入。

:%v/Like/.,/Like/-1j

如果只需要以"赞"开头的行,请使用^Like。如果你想去掉尾随空格,就按照sidyll写的去做。

代码的意思是:

 % for all lines
 v that do not match /Like/
 ., do from the current line (aka the (not) matching line)
 /Like/-1 To the line bevor the next line matching /Like/
 j join.

由于这更容易理解,看起来也更好,我只在这里添加完整版本:

$s/$/^MLike/|exec '%v/Like/.,/Like/-1j'|$d

它增加了以下内容:

$s/$/^MLike/

^M为实际回报(通过^Vreturn完成(这行在末尾添加了一个"赞",以防你没有

exec '...'

执行v线并保护最后一个|不被包括在重复中

$d

再次删除添加的"点赞"。

我将使用以下命令。

:v/Like/,/n.*Like|%$/j

最新更新