Tip #1038: Search for selected text
tip karma |
Rating 61/25, Viewed by 2562
|
Read and edit this tip on the
Vim tip wiki.
The wiki may have a more recent version of this tip.
created: |
|
November 2, 2005 4:03 |
|
complexity: |
|
basic |
author: |
|
rodrigo |
|
as of Vim: |
|
6.0 |
You'll probably know about the '*' command, that search for the word under the cursor.
But sometimes I want to search for two words, or an expression, or just a few characters, and I have to press '/' and copy them by hand.
Now, with this map I can just select the text and press '*'.
:vnoremap * y/<C-R>"<CR>
It first copies the selection to the unnamed register, and then pastes it into the search line.
Did you know that '#' works just like '*' but backwards?
So, just for completeness, this does the same with selections:
:vnoremap # y?<C-R>"<CR>
PD: For some reason, in gvim, the * in the numeric keypad doesn't get vmapped. Is it a bug?
Regards.
Rodrigo.
<< VIM sessions & Windows shell (creating project files with vim) |
disable all auto-indenting with 'set nopaste' mode (so that imap work) >>
Additional Notes
Jean-Rene David,
November 2, 2005 4:41
|
Here is something more stable that will escape weird characters. The ^M is literal. Got this years ago from I don't know where. Probably the vim mailing list.
" From an idea by Michael Naumann, Jürgen Krämer.
function! VisualSearch(direction) range
let l:saved_reg = @"
execute "normal! vgvy"
let l:pattern = escape(@", '\\/.*$^~[]')
let l:pattern = substitute(l:pattern, "\n$", "", "")
if a:direction == 'b'
execute "normal ?" . l:pattern . "^M"
else
execute "normal /" . l:pattern . "^M"
endif
let @/ = l:pattern
let @" = l:saved_reg
endfunction
vnoremap <silent> * :call VisualSearch('f')<CR>
vnoremap <silent> # :call VisualSearch('b')<CR>
|
ilya@po4ta.com,
November 2, 2005 5:45
|
On my Gvim 6.4 I don't get match highlight until I press n. I suppose that let @/ = l:pattern was supposed to fix this problem, but it does not for me.
Does anybody else have same bug? Or I'm missing something?
|
ilya@po4ta.com,
November 2, 2005 5:48
|
Forgot to add that I does not get highlight when using function, direct mapping works ok.
|
Anonymous,
November 2, 2005 12:18
|
To map * in the numeric keypad, try vnoremap <kMultiply> ...
It works, at least in my WinXP and vim 6.2
|
Anonymous,
November 2, 2005 15:57
|
I didn't know that, but I think it's better:
:vmap <kMultiply> *
And then vnoremap * to whatever you like.
|
Anonymous,
November 2, 2005 21:01
|
I have encountered same bug which 'ilya at po4ta.com' got.
When using this function my matches sometimes don't get highlighted until i've pressed 'n' or 'N'
Sometimes cursor is changing position on next match sometimes not.
I think it's a vim bug.
Vim 7.0aa MS-Windows.
|
rodrigo,
November 3, 2005 2:00
|
It looks like the highlight cannot be activated into a function. Maybe it's a feature ;-)
The following will work with Vim 6.4.
vnoremap <silent> * :call VisualSearch('f')<CR>:let &hlsearch;=&hlsearch;<CR>
vnoremap <silent> # :call VisualSearch('b')<CR>:let &hlsearch;=&hlsearch;<CR>
|
Anonymous,
November 3, 2005 2:13
|
Thank,Rodrigo.
You last note fixed situation :)
Vim 7.0aa mswin.
Nice tip!
|
shankar.r@freescale.com,
November 3, 2005 2:16
|
Somebody in the tips page, gave this mapping and it works well for strings having special characters and for
multiple lines also.
Press "v" to start the selection and use h,j,k,l to cover any number of lines, words you want and press *
Searching through the tips will give the original authors name.
vnoremap * y/\V<C-R>=substitute(escape(@@,"/\\"),"\n","\\\\n","ge")<CR><CR>
vnoremap # y?\V<C-R>=substitute(escape(@@,"?\\"),"\n","\\\\n","ge")<CR><CR>
|
zzapper: more stuffing for the goose,
November 3, 2005 4:54
|
"my map which escapes characters which would otherwise be seens as part of regexp (magic)
vmap <silent> g/ y/<C-R>=escape(@", '\\/.*$^~[]')<CR><CR>
"I also think g/ is easy to remember
|
thoma11@hotmail.com,
December 2, 2005 12:24
|
Thanks for posting all these tips guys ....
I was wondering if anybody knows how to search for a vertical block of text.
doing <Ctrl-v> for a column of text and then * ?
I searched in the tips but could not find it.
Thanks.
Thomas.
|
maor@freud.tau.ac.il,
February 6, 2006 3:32
|
Thaks for the tip
Maor Daya
|
|