Tip #171: Do you know the "g/" and "g?" commands?
tip karma |
Rating 33/12, Viewed by 2684
|
created: |
|
December 2, 2001 11:11 |
|
complexity: |
|
basic |
author: |
|
Raymond Li ([email protected]) |
|
as of Vim: |
|
5.7 |
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!
<<Repeating a sequence of commands without defining a macro |
Using Ispell on a highlighted region >>
Additional Notes
|