How can I search a word after selecting it in visual mode in Vim?

后端 未结 4 641
遇见更好的自我
遇见更好的自我 2021-01-26 02:22

Suppose I have selected a word in visual mode. Now I want to search that word in the document.

How can I do that?

Thanks in advance.

相关标签:
4条回答
  • 2021-01-26 02:35

    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#.

    0 讨论(0)
  • 2021-01-26 02:41

    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>
    
    0 讨论(0)
  • 2021-01-26 02:55

    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>
    
    0 讨论(0)
  • 2021-01-26 02:57

    I recommend https://github.com/thinca/vim-visualstar because you can use * for searching but with some selections you can run into problems.

    0 讨论(0)
提交回复
热议问题