mNo edit summary |
m (Do you know the "g/" and "g?" commands? moved to Search for visually selected text: Page moved by JohnBot to improve title) |
Revision as of 09:13, 18 October 2007
Tip: #171 - Search for visually selected text
Created: December 2, 2001 11:11 Complexity: basic Author: Raymond Li (rayli--AT--rayli.net) Version: 5.7 Karma: 83/28 Imported from: Tip#171
Directly from the Vim Todo list:
7 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
"==== END ====
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.
I've only tested this on Win2000 and Redhat Linux 7.1. I'm not really clear
on how the carriage returns are dealt with on other systems.
Anyway, enjoy!
Comments
Here's YAW (Yet Another Way): put the following lines into your <.vimrc>:
vnoremap <silent> g/ y/<C-R>=escape(--AT--", '\\/.*$^~[]')<CR><CR> vnoremap <silent> g? y?<C-R>=escape(--AT--", '\\/.*$^~[]')<CR><CR>
Use n and N to repeat searches and gv to restore the previous visual highlighting.
cec--AT--NdjOinnSi.gPsfAc.nMasa.gov
, August 26, 2003 7:59
I changed this to the following so it works with line breaks:
vmap <silent> g/ y/<C-R>=substitute(escape(--AT--", '\\/.*$^~[]'),'\n','\\n','g')<CR><CR>
Anonymous
, September 26, 2006 8:50