4

So, I have the following line where I would like to find all the {#}= and put them on a new line. Of course there can be more then 9 value entries and there is more text on the line, so I am thinking of looking for something like comma, space, one or more digits, and finally the equal sign, then add a line feed between the comma and the space.

The question is: how do I do that in vi across multiple lines?

  sion] ],{1=ValueEntry, 2=ValueEntry, 3=ValueEntry, 4=ValueEntry, 5=ValueEntry, 6=ValueEntry, 7=ValueEntry, 8=ValueEntry}]

  sion] ],{1=ValueEntry, 
  2=ValueEntry, 
  3=ValueEntry, 
  4=ValueEntry, 
  5=ValueEntry, 
  6=ValueEntry, 
  7=ValueEntry, 
  8=ValueEntry}]
  • \r adds a new line in a replacement. %s/\v(,\s)(\d\=)/\1\r\2/g will add the new line after the comma and the space – Steve Nov 26 '19 at 22:01
  • I think @Steve solution is the way to go, but if your filetype is supported by the plugin github.com/AndrewRadev/splitjoin.vim could be useful too – statox Nov 27 '19 at 10:01
4

You can add a line break by using the atom \r in the replacement part of the :s command (while in the pattern part, you would need to use \n to match a line break).

That allows to split your string using the following command:

:s/,\ze \d\+=/&\r/g

That is search for a comma (and end the match there), followed by a space, at least one number and a = and replace the comma in this case with itself (using the special atom & which corresponds to the complete match) followed by a line break (\r).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.