在Vim中,如何通过一个字符将长字符串拆分为多行?

在Vim中,如何通过一个字符将长字符串拆分为多行?,vim,Vim,我有一个很长的正则表达式字符串 (\.#.+|__init__\.py.*|\.wav|\.mp3|\.mo|\.DS_Store|\.\.svn|\.png|\.PNG|\.jpe?g|\.gif|\.elc|\.rbc|\.pyc|\.swp|\.psd|\.ai|\.pdf|\.mov|\.aep|\.dmg|\.zip|\.gz|\.so|\.shx|\.shp|\.wmf|\.JPG|\.jpg.mno|\.bmp|\.ico|\.exe|\.avi|\.docx?|\.xlsx?|\

我有一个很长的正则表达式字符串

(\.#.+|__init__\.py.*|\.wav|\.mp3|\.mo|\.DS_Store|\.\.svn|\.png|\.PNG|\.jpe?g|\.gif|\.elc|\.rbc|\.pyc|\.swp|\.psd|\.ai|\.pdf|\.mov|\.aep|\.dmg|\.zip|\.gz|\.so|\.shx|\.shp|\.wmf|\.JPG|\.jpg.mno|\.bmp|\.ico|\.exe|\.avi|\.docx?|\.xlsx?|\.pptx?|\.upart)$
我想用
|
将其拆分,并将每个组件放在新行上

最后的形式是这样的

(\.#.+|
__init__\.py.*|
\.wav|
\.mp3|
\.mo|
\.DS_Store|
... etc
我知道我可能可以作为一个宏来完成这项工作,但我认为更聪明的人可以找到更快/更简单的方法


任何提示和帮助都将不胜感激。谢谢

用自身和换行符(
\r
)替换
|
的每个实例:

(执行前确保光标位于有问题的行上)

尝试一下:

:s/|/|\r/g
以上内容将在当前行中起作用

要对整个文件执行替换,请在s之前添加一个
%

:%s/|/|\r/g
细分:

:    - enter command-line mode
%    - operate on entire file
s    - substitute
/    - separator used for substitute commands (doesn't have to be a /)
|    - the pattern you want to replace
/    - another separator (has to be the same as the first one)
|\r  - what we want to replace the substitution pattern with
/    - another separator
g    - perform the substitution multiple times per line

实际上,你不必在模式前添加
,试试这个
s/,/,\r/g
它会在换行符后用逗号替换逗号。

选择行,然后
'哦,我不知道你可以添加一个带有
\r
的换行符。相关答案…可能与原始问题或答案无关的重复,但是为什么它是
\r
而不是
\n
?@Aperçu我还没有研究为什么,但是在VIM中,你必须使用
\r
作为替换字符,它将为底层操作系统做正确的事情。如果您使用
\n
作为替换,结果将不会是预期的结果。@Apalala这是对您问题的回答,而不是问题;)@阿帕拉拉见@Aperçu Nice。我认为,你的8.5k允许你编辑答案;-)我也是;没有解释…`:`@AndrewMarshall-也许我们的答案太有效了。。。我已经投了你的票:-)
:    - enter command-line mode
%    - operate on entire file
s    - substitute
/    - separator used for substitute commands (doesn't have to be a /)
|    - the pattern you want to replace
/    - another separator (has to be the same as the first one)
|\r  - what we want to replace the substitution pattern with
/    - another separator
g    - perform the substitution multiple times per line