Vim: use string from search in replace command

regular expressionvim

I often use vim / search command to verify my regular expressions (just to see what it matches). After that I usually use the :%s replace command, where I use that regexp from search as a string to be replaced, e.g. I first look for such string:

/TP-\(\d\{5\}\)-DD-\d\{3\}

It matches exactly what I want, so I do my replace:

:%s/TP-\(\d\{5\}\)-DD-\d\{3\}/\1/g

But I have to write again entire regexp here. Usually that regexp is much longer, that's why I'm looking for solution:

Is there any existing shortcut or vim script for pasting that search pattern directly into replace command?

I use vim in terminal (no gvim).

Best Answer

In general, an empty regular expression means to use the previously entered regular expression, so :%s//\1/g should do what you want.

Related Question