How to display all results of a search in vim

前端 未结 3 1542
面向向阳花
面向向阳花 2021-01-13 13:33

When searching string with notepad++, new window opens and shows find results. I want to use this feature in vim. After googling I found out some suggestions:



        
相关标签:
3条回答
  • 2021-01-13 14:07

    I suggest lvimgrep (so you can use quickfix for :make)

    :nnoremap <F6> :lvimgrep /\M\<<C-R><C-W>\m\>/ **/*.[ch]pp **/Makefile | lopen<CR>
    

    Also, if you just wanted to find in the current file:

    :g/<pattern>/
    

    will invoke 'print' (default command) on each matching line.

    :v//               " non-matching lines
    :g//-1             " lines preceding the matching line
    :g//-1,+1          " lines around the matching line
    

    etc.

    :global is far more useful:

     :g/foo/ join       " join all lines containing foo
    

    etc.

    0 讨论(0)
  • 2021-01-13 14:22

    The requirement is actually easy. but to get user inputted pattern, you need a function.

    function! FindAll()
        call inputsave()
        let p = input('Enter pattern:')
        call inputrestore()
        execute 'vimgrep "'.p.'" % |copen'
    endfunction
    

    if you want to have a mapping, add this line:

    nnoremap <F8> :call FindAll()<cr>
    

    but as I commented under your question. % may not work for unamed buffer.

    0 讨论(0)
  • 2021-01-13 14:23

    Those two commands can be shortened and chained: :vim foo %|co. You can pull the word under the cursor like this: :vim <C-r><C-w> %|co.

    Here is a quick normal mode mapping that you can use to list all the occurrences of the word under your cursor in the quickfix window:

    nnoremap <F6> :vimgrep /<C-r><C-w>/j % <bar> cwindow<cr>
    

    You can also use :il[ist] foo to display a list of all the occurrences of foo or [I to display the same list for the word under your cursor.

    When the list is displayed, use :{line number} to jump to the corresponding line.

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