regex - Vim: Replace regular expression match on a line with another regular expression match from same line -


hi: i'm trying 'pivot' values in vim using search/replace, seem stuck.

consider following file:

key1: value1, value2, value3, ... valuen\n key2: valuea, valueb, valuec,...valuex\n ... 

i'm trying convert every ',' 'key' on line, followed newline.

so, output be:

value1:key1\n value2:key1\n value3:key1\n ... valuen:key1\n valuea:key2\n valueb:key2\n ... 

i'm not sure how replace every occurrence of regex match (',' in case) first match.

i tried:

:%s/\(^.\{-}\): \(.\{-}\),/\2:\1\r 

but replaces first occurrence of ','.

how replace every occurrence of ',' \1 each line?

thanks!

you're trying many things @ once, can done vim if insist:

:%s/\v\s*(\w+):\s*(.*)/\=substitute(submatch(2), '\v\s*%(,\s*|$)', ':' . submatch(1) . '\n', 'g')/ 

input:

key1: value1, value2, value3 key2: valuea, valueb, valuec 

output:

value1:key1 value2:key1 value3:key1  valuea:key2 valueb:key2 valuec:key2 

Comments