Better Visual Searching in Vim
If you press *
in the Vim editor it will search for the next place where the word the cursor is over appears. The #
key does the same thing, but searches backwards instead. Unfortunately, this only works for one word at a time. But if you add some code to ~/.vimrc
then you can extend those functions to work with whatever you have selected when you are in visual mode.
" Search for selected text, forwards or backwards
vnoremap <silent> * :<C-U>
\let old_reg=getreg('"')<Bar>let old_regtype=getregtype('"')<CR>
\gvy/<C-R><C-R>=substitute(
\escape(@", '/\.*$^~['), '\_s\+', '\\_s\\+', 'g')<CR><CR>
\gV:call setreg('"', old_reg, old_regtype)<CR>
vnoremap <silent> # :<C-U>
\let old_reg=getreg('"')<Bar>let old_regtype=getregtype('"')<CR>
\gvy?<C-R><C-R>=substitute(
\escape(@", '?\.*$^~['), '\_s\+', '\\_s\\+', 'g')<CR><CR>
\gV:call setreg('"', old_reg, old_regtype)<CR>
After adding this, select some text in visual mode (press v
and move the cursor around) and search for what you have selected with *
or #
.