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

How do I populate the quickfix list from any shell command?

Answer

:cexpr system('grep -rn pattern .')

Explanation

While :make and :grep populate the quickfix list, they are limited to their configured programs. The :cexpr command lets you feed arbitrary shell output into the quickfix list, as long as the output matches the errorformat. This turns any command-line tool into a quickfix source.

How it works

  • :cexpr {expr} evaluates {expr} and parses the result using errorformat to populate the quickfix list
  • system('...') runs a shell command and returns its output as a string
  • The default errorformat already understands file:line:message format, which is what grep -n produces
  • After running, use :copen to view the quickfix list and jump to matches

Example

Find all TODO comments in a project and navigate them via quickfix:

:cexpr system('grep -rn TODO src/')
:copen

Or use a linter that is not natively supported:

:set errorformat=%f:%l:%c:\ %m
:cexpr system('shellcheck -f gcc script.sh')
:copen

Now you can jump between results with :cnext and :cprev.

Tips

  • Use :caddexpr instead to append to the existing quickfix list rather than replacing it
  • Set a custom errorformat before :cexpr if your tool's output does not match the default
  • Combine with ripgrep for fast project-wide searches: :cexpr system('rg --vimgrep pattern')
  • Use :lexpr for the location list (window-local) instead of the quickfix list (global)

Next

How do I return to normal mode from absolutely any mode in Vim?