vimtricks.wiki Concise Vim tricks, one at a time.

How do I filter the quickfix list to only show entries matching a pattern?

Answer

:Cfilter /pattern/

Explanation

Vim ships with an optional built-in package called cfilter that adds :Cfilter and :Lfilter commands for narrowing down quickfix and location list entries by pattern. This is invaluable when a :grep or :make run produces hundreds of results and you only care about a subset.

How it works

First, load the package (add this to your vimrc or run it once per session):

:packadd cfilter

Then, with a quickfix list open:

:Cfilter /pattern/      " keep only entries matching pattern
:Cfilter! /pattern/     " keep only entries NOT matching pattern
:Lfilter /pattern/      " same but for the location list

Filtering is destructive — the non-matching entries are removed from the list. Navigate the filtered list with ]q / [q or :cnext / :cprev as usual.

Example

After running :grep TODO **/*.js you get 80 results across many files. To focus only on results in src/:

:Cfilter /src\//

Now the quickfix list contains only entries whose filename includes src/.

To drop test files:

:Cfilter! /\.test\./

Tips

  • Patterns match against the full quickfix entry text (filename + line content), not just filename
  • Use :colder / :cnewer to navigate between previous quickfix list versions and undo filtering
  • Add packadd cfilter to your vimrc to always have it available
  • :Lfilter is the location-list equivalent for window-local grep results

Next

How do I highlight all occurrences of a yanked word without typing a search pattern?