Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm aware that I can use / followed by a regex to search something. And I can use ? to search backwards. And I can use n and N to repeat the search forward and backward.

There are also two nice shortcuts: * or # will search for the word under the cursor (forward/backward). That's very useful! (there are also g* and g# variants)

But... Once I've selected text using visual mode (v), how can I ask Vim to search for exactly that text? A quick look at :help did not... huh... help me.

share|improve this question
1  
Related to Search for selection in Vim. –  Rodrigue Oct 8 '12 at 15:06

4 Answers 4

up vote 4 down vote accepted

I never felt the need for such a feature but, considering you can find a need for any feature on Vim, I think that should help:

Search for visually selected text

I didn't test it but, looking at the code, it seems to be exactly what you're searching for.

share|improve this answer
    
Since both of you posted the exact same answer, I'm accepting your answer because you need more reputation than akf, and because your answer has more votes. Nevertheless, I would like to thank both of you for your answers. –  Denilson Sá Sep 15 '09 at 9:59

You can yank the hightlighted text first. Then

  • /

  • Ctrlr

  • "

Which will paste what you have yanked after the /.

share|improve this answer
1  
Nice idea... except that the pasted text will be interpreted as a regex, which is not desired. :-( –  Denilson Sá Aug 22 '10 at 16:58
1  
If this is almost good enough for you, then you could do /\V<C-r>" instead. By prepending \V to your search query, you tell vim to use "very no magic". Backslashes may still cause you problems. See :help \V –  idbrii Jul 9 '12 at 6:33
1  
It might help to explain that Ctrl+R is how you access a register, and * is simply the system clipboard register. For example, you can also do: yaw to yank a word into register " (the default register), then /<C-r>" to search for that string. –  Ben Davis Sep 30 '13 at 17:47

You can find a method to create this behavior here at the vim wiki.

share|improve this answer

In my configurations on two separate machines, if I select text and then hit / it automatically searches for the selected text.

share|improve this answer
    
That's not the default behavior. Here, when I press / (while in visual mode), it just shows an empty regex prompt, waiting for me to type something. –  Denilson Sá Sep 21 '09 at 10:51
    
Odd, because I never did anything to enable it. –  Nathan Fellman Sep 21 '09 at 11:25
3  
Many distros ship a non-standard vim config as the default (probably because the standard vim config disables many useful features, mostly for vi compatibility). –  sleske Mar 23 '10 at 10:08

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.