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

How do I narrow an existing location list to only entries matching a regex without rerunning the search?

Answer

:packadd cfilter | Lfilter /pattern/

Explanation

When you already have a populated location list, rerunning the original search just to focus on a subset is slow and disruptive. The built-in cfilter plugin lets you refine that list in place using a regex, which is ideal when you are triaging a large result set. This is especially useful in review or refactor workflows where you keep broad matches first, then progressively narrow to the exact ones you want to touch.

How it works

  • :packadd cfilter loads Vim's optional cfilter plugin for the current session
  • Lfilter /pattern/ filters the current location list to entries that match pattern
  • The filter acts on list entries, not raw file content, so it is very fast after the initial search
  • You can refine repeatedly with different patterns as you narrow the target set

Example

Start with a location list created from a broad search:

src/a.lua:12: TODO(api): remove legacy parser
src/b.lua:48: TODO(ui): update dropdown behavior
src/c.lua:77: FIXME(api): handle nil response
src/d.lua:91: TODO(api): validate payload schema

Run:

:packadd cfilter | Lfilter /TODO(api)/

Now the location list is reduced to:

src/a.lua:12: TODO(api): remove legacy parser
src/d.lua:91: TODO(api): validate payload schema

Tips

  • Use Cfilter for quickfix lists and Lfilter for location lists
  • Keep broad searches broad; filter after the fact for faster iteration
  • Combine with :lolder and :lnewer to navigate list history when iterating

Next

How do I run vimgrep across a project using whatever pattern is currently in the search register?