Often when editing code, I'll select a block in visual mode and do a search and replace over the block. After I make the changes, however, it leaves visual mode. How do a new find and replace over the same selection?

link|improve this question
Well I found one way, but it's a little cumbersome: :'<,'>s/old/new/g Any way to do this w/o having to type in the '<,'> ? – labyrinth Dec 10 '10 at 17:14
feedback

2 Answers

up vote 6 down vote accepted

You may re-select the last selected visual area with "gv".

link|improve this answer
feedback

Suppose I wanted to replace Goodbye with Hello and the code below was selected:

public static void main(String[] args){
    System.out.println("Goodbye World");
}

I would type in :s/Goodbye/Hello/ and vim will replace all instances of Goodbye with Hello

It's simply a combination of vim's regular expressions and visual mode selections. When you select, it should autofill '<,'>

link|improve this answer
Sorry, that's not what I was asking. What I'm asking is how do you reuse the selection to do another find and replace on the same range as selected before? – labyrinth Dec 10 '10 at 18:24
1  
After replacing, you will get out of visual mode. type in gv and your visual mode will revert to the previous selection state. – Scott Nguyen Dec 10 '10 at 22:03
feedback

Your Answer

 
or
required, but never shown

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