用 vim 中的逗号替换 "two lines with certain patterns" 之间的所有换行符
Replace all newlines between "two lines with certain patterns" with a comma in vim
如果我必须用逗号替换模式 1 和模式 2 之间的所有行的换行符,我该怎么做?
发件人:
Pattern 1
abcd
edfgads asd
adsad
...
Pattern 2
至:
Pattern 1, abcd, edfgads asd, adsad, ..., Pattern 2
对于vim,它将是
:%s/\n/, /g
您搜索换行符:\n
并将其替换为逗号和 space:,
这是全局完成的 g
,这些选项由 /
字符。
有关 vim 中替换的更多信息,您可以在 here
中找到
怎么样
:%s/Pattern 1\_.\{-}Pattern 2/\=join(split(submatch(0), "\n"), ", ")/g
搜索
Pattern 1 # obvious
\_. # any character including newline
\{-} # repeat non-greedily (vim's way of writing *?)
Pattern 2 # obvious
替换部分不用解释就应该清楚
您可以在替换时输入行号。
:{pattern1LineNo},{pattern2LineNo}s/\n/, /g
使用Pattern 1
和Pattern 2
作为地址,参见:help cmdline-ranges
:
:/^Pattern 1/,/^Pattern 2/-1 s/\n/, /
:g/Pattern1/norm V/Pattern2^MgJ
:g/
on lines matching Pattern1, 运行 the normal
mode keystrokes:
v
isual select 就... /
搜索 Pattern2
gJ
加入 select 行,不添加空格
注意。输入 ^M
和 Ctrl-V <Enter>
,或 Ctrl-Q <Enter>
如果我必须用逗号替换模式 1 和模式 2 之间的所有行的换行符,我该怎么做?
发件人:
Pattern 1
abcd
edfgads asd
adsad
...
Pattern 2
至:
Pattern 1, abcd, edfgads asd, adsad, ..., Pattern 2
对于vim,它将是
:%s/\n/, /g
您搜索换行符:\n
并将其替换为逗号和 space:,
这是全局完成的 g
,这些选项由 /
字符。
有关 vim 中替换的更多信息,您可以在 here
中找到怎么样
:%s/Pattern 1\_.\{-}Pattern 2/\=join(split(submatch(0), "\n"), ", ")/g
搜索
Pattern 1 # obvious
\_. # any character including newline
\{-} # repeat non-greedily (vim's way of writing *?)
Pattern 2 # obvious
替换部分不用解释就应该清楚
您可以在替换时输入行号。
:{pattern1LineNo},{pattern2LineNo}s/\n/, /g
使用Pattern 1
和Pattern 2
作为地址,参见:help cmdline-ranges
:
:/^Pattern 1/,/^Pattern 2/-1 s/\n/, /
:g/Pattern1/norm V/Pattern2^MgJ
:g/
on lines matching Pattern1, 运行 thenormal
mode keystrokes:v
isual select 就.../
搜索 Pattern2gJ
加入 select 行,不添加空格
注意。输入
^M
和Ctrl-V <Enter>
,或Ctrl-Q <Enter>