Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
Related to Search for selection in Vim. – Rodrigue Oct 8 '12 at 15:06
up vote 6 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
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
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
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
@DenilsonSá - You can use this solution, but instead of using / to do your search, use ? instead, since reverse searches escape slashes by default. Then you can hit N to continue your search in the forward direction. – Brad Parks May 8 '15 at 18:24

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
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .