How do I search for the selected text?

有些话、适合烂在心里 提交于 2020-01-10 17:45:53

问题


How do I search for the selected (with v and y) string? I usually search with /, but I cannot paste the selected string after /.


回答1:


In insert mode you can use CTRL-R to insert the contents of Vim registers. By default, copied text gets put in the unnamed register ", so to insert that text you would type <C-R>" in insert mode. The search prompt uses Command-line mode which has its own CTRL-R that works almost identically to the one in Insert mode.

So if I just yanked the text foo, typing /<C-R>" would search for the text foo once I press enter.




回答2:


I have this mapping defined in my vimrc, it maps * to defining the search pattern as what is currently highlighted (escaping all potential dangerous characters, and converting a space in what is highlighted to any sequence of spaces)

xnoremap * :<C-U>let old_reg=getreg('"')|let old_regtype=getregtype('"')<CR>gvy/<C-R><C-R>=substitute(substitute(escape(@", '/\.*$^~['), '\s\+', '\\s\\+', 'g'), '\_s\+', '\\_s*', 'g')<CR><CR>gV:call setreg('"', old_reg, old_regtype)<CR>:let v:searchforward=1<CR>

In order to use it, start visual mode with v, and then highlight what you want to search and press * not y.

Of course you can map # to search backwards (exactly the same except that v:searchforward should be set to 0.




回答3:


:set hls
:vmap * y:let @/ = @"<CR>
  • set hls (hight light search)
  • v => hjkl (select something)
  • press *, copy selected text to reg "
  • set content of reg / as "
  • press n/N to navigate



回答4:


If you only care about searches, you can use the cheat method I use. Yank a word via v or y+w, then issue the command

:%s//XYZ/gc 

This will search for the last searched word. Then when you find it, it will ask for confirmation to replace with XYZ, and all you have to do is hit q to quit.



来源:https://stackoverflow.com/questions/8587136/how-do-i-search-for-the-selected-text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!