Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

regex

replace

vim

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?

like image 579
Kelvin Avatar asked Dec 20 '22 03:12

Kelvin


1 Answers

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)
like image 126
Kent Avatar answered Jan 12 '23 01:01

Kent