Last Updated: February 25, 2016
·
333
· kablamo

Search for highlighted text with *

In your .vimrc

Add these key maps to your .vimrc:

vmap * y/<c-r>"<cr>
vmap # y?<c-r>"<cr>

How to use it

  • Select some text in visual mode.
  • Hit *, to find the next occurance.
  • Hit # to find the previous one.

How it works

y/<c-r>"<cr> does the following:

  • y yanks the currently highlighted text to the default register which is ".
  • / begins a search
  • <c-r> allows you to insert the contents of a register. In this case we are insert the contents of the unamed registers ". This is very powerful and you can use this in a search or on the vim commandline.
  • <cr> carriage return -- same as hitting enter.

I got this trick out of the vim docs