Chan
Chan

Reputation: 1969

How to replace the character I want in a line

1 aaa bbb aaa
2 aaa ccccccccc aaa
3 aaa xx aaa

How to replace the second aaa to yyy for each line

1 aaa bbb yyy
2 aaa ccccccccc yyy
3 aaa xx yyy

Upvotes: 0

Views: 100

Answers (3)

Alan Gómez
Alan Gómez

Reputation: 378

Alternatively:

:%s/aaa.*\zsaaa/yyy

Without using \ze command.

Upvotes: 0

kenshinji
kenshinji

Reputation: 2091

Issuing the following command will solve your problem.

  :%s/\(aaa.\{-}\)aaa/\1yyy/g

Upvotes: 2

Pandu
Pandu

Reputation: 1686

Another way would be with \zs and \ze, which mark the beginning and end of a match in a pattern. So you could do:

:%s/aaa.*\zsaaa\ze/yyy

In other words, find "aaa" followed by anything and then another "aaa", and replace that with "yyy".

If you have three "aaa"s on a line, this won't work, though, and you should use \{-} instead of *. (See :h non-greedy)

Upvotes: 1

Related Questions