I want to do the following in vim.
a,2 with a@,a@b,3 with b@,b@,b@c,6 with c@,c@,c@,c@,c@,c@
and so onThat's another case for the powerful :help sub-replace-expression:
:%s/\(\w\),\(\d\+\)/\=join(repeat([submatch(1) . '@'], submatch(2)), ',')/g
This matches a word character (\w) followed by , and a number, appends @ to each matched word character (submatch(1) . '@), turns that into a List ([...]), multiplies the List element according to the matched number (repeat()), then join()s that back into a ,-separated string, which is used as the replacement.
This is based on your example; you need to tweak the pattern according to your particular needs.
vim has repeat() function. This command should do what you want:
%s/\v([a-z]),(\d+)/\=substitute(repeat(submatch(1).'@,',submatch(2)),',$','','g')/g
Note that this line will change foo,3abc into foo@,o@,o@abc. The result might be not exactly what you are looking for, but by reading the command, you should know the idea, you can change it to make it fit your needs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With