How can I search a word after selecting it in visual mode in Vim?
Suppose I have selected a word in visual mode. Now I want to search that word in the documen开发者_开发百科t.
How can I do that?
Thanks in advance.
If it's just a single word, you don't even have to select it. Just place the cursor on the word and press * (or # to search backwards). Note that this search will only match the whole word. To allow a search for foo
to match foobar
, use g* or g#.
Press y (you'll exit from visual mode after that) then press / Ctrl+r then " end hit enter.
You can use it to bind // for this action:
:vmap // y/<C-R>"<CR>
If you select special chars you better use this
:vmap <silent> // y/<C-R>=escape(@", '\\/.*$^~[]')<CR><CR>
I recommend https://github.com/thinca/vim-visualstar because you can use * for searching but with some selections you can run into problems.
I have this in my ~/.vimrc
:
" Search for visually-selected text, forwards or backwards.
vnoremap <silent> * :<C-U>
\let old_reg=getreg('"')<Bar>let old_regtype=getregtype('"')<CR>
\gvy/<C-R><C-R>=substitute(
\escape(@", '/\.*$^~['), '\_s\+', '\\_s\\+', 'g')<CR><CR>
\gV:call setreg('"', old_reg, old_regtype)<CR>
vnoremap <silent> # :<C-U>
\let old_reg=getreg('"')<Bar>let old_regtype=getregtype('"')<CR>
\gvy?<C-R><C-R>=substitute(
\escape(@", '?\.*$^~['), '\_s\+', '\\_s\\+', 'g')<CR><CR>
\gV:call setreg('"', old_reg, old_regtype)<CR>
精彩评论