如何连接添加分隔符的行? [英] How to join lines adding a separator?
问题描述
命令J
连接行.
命令gJ
连接删除空格的行
The command J
joins lines.
The command gJ
joins lines removing spaces
是否还有一个命令来连接线,在线之间添加分隔符?
Is there also a command to Join lines adding a separator between the lines?
示例:
输入:
text
other text
more text
text
我想做什么:
-选择这4行
-如果开始时有空格和/或EOL,请删除它们
-连接线之间添加分隔符'//'
What I want to do:
- select these 4 lines
- if there are spaces at start and/or EOL remove them
- join lines adding a separator '//' between them
输出:
text//other text//more text//text
推荐答案
您可以为此使用:substitute
,并在\n
上进行匹配:
You can use :substitute
for that, matching on \n
:
:%s#\s*\n\s*#//#g
但是,这也会在末尾追加分隔符(因为该范围的最后一行也有换行符).您可以手动删除该标记,或者指定c
标志并在最后一个标记之前退出替换,或者将范围缩小一个,而将:join
缩小到最后一个标记:
However, this appends the separator at the end, too (because the last line in the range also has a newline). You could remove that manually, or specify the c
flag and quit the substitution before the last one, or reduce the range by one and :join
the last one instead:
:1,$-1s#\s*\n\s*#//#g|join
这篇关于如何连接添加分隔符的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!