m (Do you know the "g/" and "g?" commands? moved to Search for visually selected text: Page moved by JohnBot to improve title) |
(Uploaded by JohnBot from a locally edited file) |
||
Line 1: | Line 1: | ||
− | |||
{{review}} |
{{review}} |
||
+ | {{TipImported |
||
− | {{Tip |
||
|id=171 |
|id=171 |
||
+ | |previous=170 |
||
− | |title=Do you know the "g/" and "g?" commands? |
||
+ | |next=172 |
||
− | |created=December 2, 2001 |
+ | |created=December 2, 2001 |
|complexity=basic |
|complexity=basic |
||
− | |author=Raymond Li |
+ | |author=Raymond Li |
|version=5.7 |
|version=5.7 |
||
|rating=83/28 |
|rating=83/28 |
||
⚫ | |||
− | |text= |
||
− | Directly from the Vim Todo list: |
+ | Directly from the Vim Todo list: |
+ | For Visual mode: Command to do a search for the string in the marked area. |
||
⚫ | |||
− | + | In other words, a way to search for visually selected text !! :-) |
|
+ | <pre> |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | </pre> |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | In other words, a way to search for visually selected text !! :-) |
||
+ | 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. |
||
⚫ | |||
⚫ | |||
+ | <pre> |
||
⚫ | |||
⚫ | |||
⚫ | |||
+ | </pre> |
||
⚫ | |||
− | |||
⚫ | |||
− | |||
− | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
− | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
− | |||
− | |||
− | "==== END ==== |
||
− | |||
− | |||
− | |||
− | |||
− | |||
⚫ | |||
− | |||
− | sourced. If not, you must manually source this file using ':source vsearch.vim'. |
||
− | |||
− | |||
− | |||
⚫ | |||
− | |||
− | default visual key mappings |
||
− | |||
− | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
− | |||
− | |||
− | Visual searches behave like normal searches. The 'n' and 'N' commands |
||
− | |||
⚫ | |||
− | |||
− | 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! |
||
⚫ | |||
− | |||
⚫ | |||
⚫ | |||
− | |||
⚫ | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
− | |||
− | cec--AT--NdjOinnSi.gPsfAc.nMasa.gov |
||
− | , August 26, 2003 7:59 |
||
---- |
---- |
||
− | I changed this to the following so it works with line breaks: |
+ | I changed this to the following so it works with line breaks: |
− | |||
⚫ | |||
+ | <pre> |
||
⚫ | |||
+ | </pre> |
||
− | '''Anonymous''' |
||
− | , September 26, 2006 8:50 |
||
---- |
---- |
||
− | <!-- parsed by vimtips.py in 0.279957 seconds--> |
Revision as of 10:48, 30 October 2007
created December 2, 2001 · complexity basic · author Raymond Li · version 5.7
Directly from the Vim Todo list:
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
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.
Comments
Here's YAW (Yet Another Way): put the following lines into your <.vimrc>:
vnoremap <silent> g/ y/<C-R>=escape(@", '\\/.*$^~[]')<CR><CR> vnoremap <silent> g? y?<C-R>=escape(@", '\\/.*$^~[]')<CR><CR>
Use n and N to repeat searches and gv to restore the previous visual highlighting.
I changed this to the following so it works with line breaks:
vmap <silent> g/ y/<C-R>=substitute(escape(@", '\\/.*$^~[]'),'\n','\\n','g')<CR><CR>