Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to grep the quickfix window in Vim?

Tags:

grep

vim

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 first_file.rb|1 col 1| disabled something something
2 second_file.rb|1 col 2| disabled another something

Is it possible to pick the quickfix results as an input, grep through them, and open results in new quickfix window? In other words, if I would enter :quickfix_grep first_file, new quickfix would pop up with only one entry:

1 first_file.rb|1 col 1| disabled something something
like image 347
Arnis Lapsa Avatar asked Mar 14 '13 10:03

Arnis Lapsa


People also ask

How do I open a quickfix list?

See the demo below. While there are several commands for navigating the quickfix list, these are the most common ones that you need to be aware of: :copen - Open the quickfix list window. :ccl or :cclose - Close the quickfix list window.

How do I grep in Vim?

You simply type :Grep foobar , and it will search in your current directory through all file extensions (except json and pyc; you can add more to the blacklist). It also displays the results in a nice little buffer window, which you can navigate through with normal HJKL keys, and open matches in the main editor window.

What is Vimgrep?

Unlike normal within-file search, vimgrep (or rather, the quickfix list) tells you which match you're on (e.g. 2 of 5 ). If you want to search within the current file but have this feature, tell vimgrep to search the current file with % : :vimgrep /foo/g %


2 Answers

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.

like image 52
Kent Avatar answered Oct 11 '22 11:10

Kent


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
like image 38
Hotschke Avatar answered Oct 11 '22 11:10

Hotschke