Is it possible to grep the quickfix window in Vim?

后端 未结 4 1448
旧时难觅i
旧时难觅i 2020-12-30 08:11

Let’s say I use the ag.vim plugin to search for the string disabled through multiple files. It returns me some results in the quickfix window:

1          


        
相关标签:
4条回答
  • 2020-12-30 08:58

    Here's a shorter & neater version of @Kent's answer:

    function! GrepQuickFix(pat)
      call setqflist(filter(getqflist(), "bufname(v:val['bufnr']) !~# a:pat"))
    endfunction
    command! -nargs=* GrepQF call GrepQuickFix(<q-args>)
    

    It is the same code, just neater and shorter, I don't believe it deserves a separate plugin.

    0 讨论(0)
  • 2020-12-30 09:07

    Update

    A vim plugin has been written for this requirement: https://github.com/sk1418/QFGrep


    Original Answer:

    My understanding of your goal is:

    Your grep result is somehow huge in your quickfix, you want to narrow your view of it. by entering a command with regex, filter the grep result. The filtered result should also be displayed in QuickFix window, so that you could open/jump to the file.

    If the above is what you want, check out the following:

    source this function and the command line:

    function! GrepQuickFix(pat)
      let all = getqflist()
      for d in all
        if bufname(d['bufnr']) !~ a:pat && d['text'] !~ a:pat
            call remove(all, index(all,d))
        endif
      endfor
      call setqflist(all)
    endfunction
    command! -nargs=* GrepQF call GrepQuickFix(<q-args>)
    

    then after your grep/ack/whatever show stuffs in your quickfix, you could type

    :GrepQF <regex>
    

    to do filtering in your quickfix.

    Here I add an GIF animation. I am using Ack instead of grep, but it makes no difference. The given regex will match filename and the text showing in quickfix. I did filtering twice to show that.

    enter image description here

    hope it helps.

    0 讨论(0)
  • 2020-12-30 09:07

    My solution to this problem has always been to make the quickfix buffer modifiable by default:

    :autocmd BufReadPost quickfix set modifiable
    

    (The above command is to be put in the .vimrc file.)

    Doing this opens a whole range of possibilities for any appropriate edits, such as removing unrelated entries manually or by filtering using the :global and :vglobal commands, grouping and reordering related entries, adding blank lines or comments in free form, etc.

    0 讨论(0)
  • 2020-12-30 09:13

    New official vim plugin cfilter

    Since 21.8.2018 (patch: 8.1.0311) the plugin cfilter is distributed with vim in $VIMRUNTIME. It is documented under :h cfilter-plugin.

    Load plugin cfilter when needed or load it always in your vimrc

    :packadd cfilter
    

    Filter quickfix list with

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