Vim Tips Wiki
Register
(Added some rather verbose comments for how this works; let me know if I went overboard. :))
m (commented out some tips in the to-be-merged section that I already handled merging.)
Line 39: Line 39:
 
Tips related to visual searching (need to merge):
 
Tips related to visual searching (need to merge):
 
*171 Search for visually selected text ''(this tip)''
 
*171 Search for visually selected text ''(this tip)''
*[[VimTip340|340 Visual select and search]]
+
<!-- *[[VimTip340|340 Visual select and search]] -->
*[[VimTip777|777 More words searching]]
+
<!-- *[[VimTip777|777 More words searching]] -->
 
*[[VimTip780|780 Generalized VISUAL CONTENT onto COMMAND-LINE]]
 
*[[VimTip780|780 Generalized VISUAL CONTENT onto COMMAND-LINE]]
 
*[[VimTip1011|1011 Mappings and commands for visual mode]]
 
*[[VimTip1011|1011 Mappings and commands for visual mode]]
 
<!-- *[[VimTip1038|1038 Search for selected text]] -->
 
<!-- *[[VimTip1038|1038 Search for selected text]] -->
 
*[[VimTip1151|1151 Search visually]]
 
*[[VimTip1151|1151 Search visually]]
*[[VimTip1387|1387 Standard editing shortcuts]] (contains info that should be here)
+
<!-- *[[VimTip1387|1387 Standard editing shortcuts]] (contains info that should be here) -->

Revision as of 08:05, 13 July 2008

Tip 171 Printable Monobook Previous Next

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>

This code first defines a function, only accessible inside the script it's defined in (:help s:), which first backs up the unnamed register (:help expr-register) so it we can restore it, then yanks the visual selection into the unnamed register (:help :norm :help gv :help y). Then, it takes that string and escapes all \ to \\, and replaces every newline with a pattern that matches newlines (:help escape() :help substitute()), and finally stores the resulting string to the search pattern register (:help registers), prepended with \V to turn off the special meanings of all characters but \ (which we already escaped all instance of) (:help /\V). Finally, the function restores the unnamed register to the value it had when we started. The visual map for * presses : in visual mode, then <C-u> in command line mode to remove the '<,'> that : inserts in visual mode (:help c_CTRL-u). Then, it calls the function to set up the search pattern register (:help :call :help <SID>), and then searches forwards for the next instance of the pattern (:help /<CR>). The visual map for # does the same, but searches backwards instead (:help ?<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. To make the * key on the numeric keypad also trigger this mapping, you can add this line:

vmap <kMultiply> *

Comments

 TO DO 
Tips related to visual searching (need to merge):