InternazionalIV
InternazionalIV

Reputation: 55

(vim) regex: masking text with help of pattern

Am i correct to understand, that the definition

:range s[ubstitute]/pattern/string/cgiI

suggests that in the string part indeed only strings are to be used, that is patterns not allowed? What i would like to do is do replacement of say any N symbols at position M with X*N symbols, so i would have liked to use something like this:

:%s/^\(.\{10}\).\{28}/\1X\{28}/g

Which does not work because \{28} is interpreted literally.

Is writing the 28 XXXXX...X in the replace part the only possibility?

Upvotes: 2

Views: 288

Answers (3)

Ingo Karkat
Ingo Karkat

Reputation: 172510

You can use expressions in the replacement part via \=. You have to access the match via submatch(), and join it together with the static string, which you can generate via repeat():

:%s/^\(.\{10}\).\{28}/\=submatch(1) . repeat('X',28)/g

Upvotes: 2

dusan
dusan

Reputation: 9273

Another alternative is using a expression in the replacement part:

:%s/^\(.\{10}\).\{28}/\=submatch(1).repeat("X",28)/g

The first matched group is obtained with submatch(1). For more information see :h sub-replace-expression.

Upvotes: 1

Ned Batchelder
Ned Batchelder

Reputation: 375484

The only regex constructs allowed in the replacement part are numbered groups: \1 \2 \3 etc. The repeating construct {28} is not valid there, though it's a clever idea. You'll have to use 28 X's.

Upvotes: 2

Related Questions