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).
In general, an empty regular expression means to use the previously entered regular expression, so :%s//\1/g
should do what you want.
/
to insert the contents of the search pattern register (/
) directly into a partially typed command line (e.g. right after :%s/
). — Mar 13, 2011 at 05:10 \9
, there is no ambiguity. — Aug 25, 2017 at 12:47