MaMu
MaMu

Reputation: 1869

Simple substitution

How do I substitute &&&&&&&&& with &? I have tried :%s/&&&&&&&&&/&/gbut i only get more &.

Upvotes: 2

Views: 103

Answers (4)

jkshah
jkshah

Reputation: 11703

You need to escape & with \ in replace string

:%s/&&&&&&&&&/\&/g

In replace string, & has a special meaning and contains matching string.

Therefore in you case, you are replacing nothing but match itself hence no change.


If your intension is to replace multiple & with a single one, then try following

:%s/&\+/\&/g

Upvotes: 4

Daan
Daan

Reputation: 31

You need a quantifier in the expression:

:%s/&\+/&/g

not sure if it works like that in vim, it is default regex, the plus sign tells it should capture any combination of one or multiple '&' signs, using &{9} should find exactly nine

Upvotes: -3

Jo So
Jo So

Reputation: 26501

You must escape & in the replacement section. Unescaped, & refers to the whole match.

:%s/&&&&&&&&&/\&/g

Upvotes: 4

T.J. Crowder
T.J. Crowder

Reputation: 1074295

Because & is special in the replacement part (it means "the whole matched string"), you have to escape it:

:%s/&&&&&&&&&/\&/g

(Note the backslash before & in the replacement part.)

Upvotes: 5

Related Questions