Kelvin
Kelvin

Reputation: 20932

In vim, how do I copy the regular expression pattern?

Sometimes I do a regexp search with /. But then I want to use the same pattern to do a replace, with %s. How do I copy or otherwise reuse the pattern?

Upvotes: 4

Views: 123

Answers (1)

Kent
Kent

Reputation: 195269

for example, you did a

/foo

then you could:

%s//bar/g

vim will replace all foo (your last search) with bar

Your last search pattern was stored in / register, you could get it by reading the register value. e.g.

"/p (in normal mode)

or

<c-r>/ (in insert/command mode)

Upvotes: 11

Related Questions