Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

P.S. I use vim in terminal (no gvim).

share|improve this question

2 Answers 2

up vote 13 down vote accepted

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

share|improve this answer
    
Woah, it's that easy! Exactly what I needed, thanks. –  bigfun Mar 12 '11 at 23:49
6  
Also, if you want to verify or modify the last used pattern, you can use Control-r then / to insert the contents of the search pattern register (/) directly into a partially typed command line (e.g. right after :%s/). –  Chris Johnsen Mar 13 '11 at 5:10
    
thats very useful hint as well. Thanks @Chris! +1 –  bigfun Mar 13 '11 at 10:38

Another good way to see past reg-exes searches as well as Ex commands and make changes to them is to edit these commands in normal mode. From this Mode you will have all your vim powers including copy and paste.

Searching

rather than
? or / for searching try
q/ or q?

for Ex Commands

rather than
:
try
q:

to exit this mode use CTRL-C

To read more see :help q:

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.