sponsor Vim development Vim logo Vim Book Ad

basic Tip #1501: substitute last search

 tip karma   Rating 402/159, Viewed by 9643 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   February 2, 2007 6:14      complexity:   basic
author:   Jerome      as of Vim:  

When I want to do a complex substitution with regular expression, I usually do a previous search of what I want to replace. Doing so, make me sure that I haven’t made any mistake, but if the regular expression is a bit long, it is boring to type it a second time…

Here is a trick to bypass this problem:

/<regular expression which match what I’m going to replace>
:%s/<c-r>//<new string>/g

<c-r>/ will be replaced by my previous search regular expression.

 rate this tip  Life Changing Helpful Unfulfilling 

<< By default, when opening files in Mac OS X, a new vim window is opened. This shows you how to have only one window. | External commands on Windows >>

Additional Notes

Anonymous, February 5, 2007 8:02
%s//.../
now this is a great  tip!  I had never seen it before, and it's immediately useful.

And also today, I learned of a Vi input manager for Cocoa applications on the Macintosh:
http://www.corsofamily.net/jcorso/vi/

It isn't the everything in Vi, let alone Vim, but it's a fantastic start.

This has been a great Vim day.
info at bertram dash scharpf dot de, February 2, 2007 10:04
Leaving the pattern empty has almost the same effect.

%s//.../

Maybe you like to have a look at "&", ":&", and "<c-r><c-w>".
Anonymous, February 5, 2007 3:47
Thanks, it is really interesting ...
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
SourceForge.net Logo