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 usingerrorformatto populate the quickfix listsystem('...')runs a shell command and returns its output as a string- The default
errorformatalready understandsfile:line:messageformat, which is whatgrep -nproduces - After running, use
:copento 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
:caddexprinstead to append to the existing quickfix list rather than replacing it - Set a custom
errorformatbefore:cexprif your tool's output does not match the default - Combine with
ripgrepfor fast project-wide searches::cexpr system('rg --vimgrep pattern') - Use
:lexprfor the location list (window-local) instead of the quickfix list (global)