Vim Tips Wiki
Register
(Removed all the bad solutions, combined them all into one solution that doesn't clobber any variables or registers, properly quotes all characters, and handles searches over an EOL.)
m (fixed an unclosed teletype tag, added a small map for making keypad multiply behave like *)
Line 28: Line 28:
 
</pre>
 
</pre>
   
You can either put this code into a file in your <tt>plugins<tt> directory (make sure the filename ends in '.vim') or include it directly in your .vimrc.
+
You can either put this code into a file in your <tt>plugins</tt> directory (make sure the filename ends in '.vim') or include it directly in your .vimrc. To make the <tt>*</tt> key on the numeric keypad also trigger this mapping, you can add this line:
  +
<pre>
  +
vmap <kMultiply> *
  +
</pre>
   
 
==Comments==
 
==Comments==

Revision as of 06:57, 12 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>

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):