25

I have this long regex string

(\.#.+|__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)$

and I would like to split it by | and have each component on a new line.

So something like this in the final form

(\.#.+|
__init__\.py.*|
\.wav|
\.mp3|
\.mo|
\.DS_Store|
... etc

I know I can probably do this as a macro, but I figured someone smarter can find a faster/easier way.

Any tips and helps are appreciated. Thanks!

4

3 回答 3

64

试试这个:

:s/|/|\r/g

以上将适用于当前行。

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

:%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
于 2013-05-31T06:06:43.903 回答
19

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

:s/|/|\r/g

(确保您的光标在执行前位于相关行上)

于 2013-05-31T06:06:31.590 回答
-4

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

于 2015-11-19T04:35:39.223 回答