sponsor Vim development Vim logo Vim Book Ad

basic Tip #439: Replace text in highlighted search

 tip karma   Rating 16/6, Viewed by 1798 

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

created:   March 9, 2003 11:53      complexity:   basic
author:   Thomas Nickl      as of Vim:   5.7

you can search for text and highlight it, using  /pattern
usually you want to replace the exact found occourences with another string, because the common trick to e.g. repeadetly pressing:
1. cw = change till end of word, or ct(  = change till first occourence of character (
2. n = next found pattern
3. . = do command 1. again
3. goto 2.
is too cumbersome, besides, you already specified what you want to replace with /pattern.


use c//e as 1. (it replaces till end of highlight).
you will NEED to bind:
nnoremap n //<CR>
nnoremap N ??<CR>
in your vimrc, or step 2. will end up with the cursorposition on the last character of your match, because it will remember the pseudo-search-command in step 1.

 rate this tip  Life Changing Helpful Unfulfilling 

<< Search/replace "within selection" in gvim using '< '> | Automatic formatting of paragraphs >>

Additional Notes

janjaapsandee@hotmail.com, March 14, 2003 5:10
I usually just do


the 1,$ can be replaced with any valid range of line numbers.

Works quite fast. Except when certain strings should not be replaced.
Anonymous, April 23, 2003 11:58
If certain strings should not be replaced, just add a 'c' (for confirm) at the end of your expression:


This way, vim will ask you before each replace.
Anonymous, November 22, 2004 4:41


If no pattern is given, :substitute uses the last search pattern ("/).  This can be very handy if you want to do a :substitute based on a complex regexp and you want to test the regexp before actually doing the :substitute.  Simply do a search first with the regexp, note if what you wanted is highlighted, & then (assuming you want to change all occurrences in the buffer):

Anonymous, May 15, 2005 18:06
And there's always ":s/<C-R>//Replace/gc" where <C-R>/ is the key sequence control-R followed by slash, which will pull the text of the search register into the command line.
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.
Sponsored by Web Concept Group Inc. SourceForge.net Logo