Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Vim how do I split a long string into multiple lines by a character?

Tags:

vim

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!

like image 993
hobbes3 Avatar asked Aug 31 '25 15:08

hobbes3


2 Answers

Give this a try:

:s/|/|\r/g

The above will work on the current line.

To perform the substitution on the entire file, add a % before the s:

:%s/|/|\r/g

Breakdown:

:    - 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
like image 132
jahroy Avatar answered Oct 14 '25 03:10

jahroy


Replace each instance of | with itself and a newline (\r):

:s/|/|\r/g

(ensure your cursor is on the line in question before executing)

like image 21
Andrew Marshall Avatar answered Oct 14 '25 02:10

Andrew Marshall