created December 2, 2001 · complexity basic · author Raymond Li · version 5.7
With the following, you can use * (or #) to search forwards (or backwards) for the current visual selection from either characterwise visual mode or linewise visual mode (but not from blockwise visual mode). These visual searches behave like any other searches; the 'n' and 'N' commands work as they should, and the search history correctly records each search. This solution works for all characters, and even for searches that span multiple lines (that is, if you select "a" at the end of one line and "b" at the beginning of the next, we'll only find other lines that end in "a" and have "b" as the first character on the next line).
" vsearch.vim " Visual mode search function! s:VSetSearch() let temp = @@ norm! gvy let @/ = '\V' . substitute(escape(@@, '\'), '\n', '\\n', 'g') let @@ = temp endfunction vnoremap * :<C-u>call <SID>VSetSearch()<CR>/<CR> vnoremap # :<C-u>call <SID>VSetSearch()<CR>?<CR>
You can either put this code into a file in your plugins directory (make sure the filename ends in '.vim') or include it directly in your .vimrc.
Comments
TO DO
Tips related to visual searching (need to merge):
- 171 Search for visually selected text (this tip)
- 340 Visual select and search
- 777 More words searching
- 780 Generalized VISUAL CONTENT onto COMMAND-LINE
- 1011 Mappings and commands for visual mode
- 1151 Search visually
- 1387 Standard editing shortcuts (contains info that should be here)