I would like to exclude results of a vimgrep search pattern from the quick fix list. The exclusions should be for all files that are under a directory or parent directory called "xyz".

Example: hello/how/xyz/are/you/ty.c or hello/how/are/xyz/you/ty.c should be excluded in the quick fix list from the search results in quick fix list of a string that is contained in ty.c


Use the Cfilter plugin that comes with vim:

:packadd cfilter
:Cfilter! /xyz/

(more at :help cfilter-plugin)

See :colder and :cnewer to move between different versions of the quickfix list.

  • Why does :Cfilter! /Xyz/ not work when I add it to my vimrc...but works after I vimgrep and enter it as a command in vim, is there any way to make it work from vimrc? – Dsp guy sam Mar 26 at 13:16
  • You can put the packadd in your vimrc, but the commands filter the current quickfix list. So if there is no current qf list, it doesn’t do anything; and, it won’t be applied every time the qf list changes. For that you would need an autocommand and that seems likely to get in the way when you don’t want it. – D. Ben Knoble Mar 26 at 14:12
  • 1
    I think I'll just create a normal mode map shortcut for this, that would be easier – Dsp guy sam Mar 26 at 14:15

:vimgrep can take multiple file globs. In simple cases that means you can potentially search only places you want instead of excluding (think white-listing vs black-listing)

:vimgrep /foo/ bar/** baz/**

Often :vimgrep isn't up to the task from performance and/or complex searching perspective. Instead you can use Vim's :grep command to use an external command like grep, ag, rg, ack, or git-grep by setting 'grepprg' and 'grepformat'.

An example of using ripgrep to exclude zxy and only search in c files for pattern, pat:

$ rg -g '!zyx' -t c 'pat'

With a few vimrc settings you use :grep:

:set grepprg=rg\ --vimgrep
:set grepformat^=%f:%l:%c:%m

Inside Vim you can now use ripgrep and :grep together:

:grep -g '!zyx' -t c 'pat'

Of course as D. Ben Knoble said above, you can always use :Cfilter to filter/exclude from the quickfix after the fact

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.