odessos
odessos

Reputation: 243

How to replace n occurrences of a char with n-m occurrences of the same char

All is in the title. I can give an example: Assume I want to replace 75 times c with 30 times c

I know this is something like :%s#c\{75}#???#g, but I don't find the ??? part

Upvotes: 1

Views: 58

Answers (3)

Ben
Ben

Reputation: 8905

How about instead replacing (n-m)+m characters, with (n-m) characters?

:%s;\(c\{45}\)c\{30};\1;g

Upvotes: 1

romainl
romainl

Reputation: 196556

This substitution should do the trick:

:%s/\(c\)\{75}/\=repeat(submatch(1),30)/g

The pattern is enclosed in a group for use with submatch() which is then repeated 30 times with repeat().

Upvotes: 4

Kent
Kent

Reputation: 195059

one way to go is using macro.

qq/c\{75}<cr>45xq

then

x@q

x is how many times you want to do the replacement.

if you don't know the times, you can use recursive macro: qq/c\{75}<cr>45x@qq then @q

Upvotes: 1

Related Questions