created December 2, 2001 · complexity basic · author Raymond Li · version 5.7
Directly from the Vim Todo list:
For Visual mode: Command to do a search for the string in the marked area.
Only when less than two lines. Use "g/" and "g?".
In other words, a way to search for visually selected text !! :-)
" vsearch.vim " Visual mode search vmap g/ :call VsearchPatternSave()<cr>/<c-r>/<cr> vmap g? :call VsearchPatternSave()<cr>?<c-r>/<cr> function! VsearchPatternSave() let l:temp = @@ normal gvy let @/ = substitute(escape(@@, '/'), "\n", "\\\\n", "g") let @@ = l:temp unlet l:temp endfunction
Normally, this file should reside in the plugins directory and be automatically sourced. If not, you must manually source this file using ':source vsearch.vim'.
In Visual mode, highlight the text for searching. Then you can use the default visual key mappings
g/ - search forwards g? - search backwards
Visual searches behave like normal searches. The 'n' and 'N' commands work as they should, and the search history correctly records each search.
Multi-line searches behave as they should (this corrects the 'yank-only' method mentioned in the Vim help files). Block visual searches do not work yet. Hopefully, someone can figure out a way to do this easily.
Comments
Here's YAW (Yet Another Way): put the following lines into your <.vimrc>:
vnoremap <silent> g/ y/<C-R>=escape(@", '\\/.*$^~[]')<CR><CR> vnoremap <silent> g? y?<C-R>=escape(@", '\\/.*$^~[]')<CR><CR>
Use n and N to repeat searches and gv to restore the previous visual highlighting.
I changed this to the following so it works with line breaks:
vmap <silent> g/ y/<C-R>=substitute(escape(@", '\\/.*$^~[]'),'\n','\\n','g')<CR><CR>