How can I start a build with my makeprg running in a different directory than the working directory of vim and still catch errors in quickfix so that I can jump between them?

The problem is that quickfix doesn't know how to find the error locations as reported by the makeprg due to the different paths. (The particular build system I have in mind is CMake and Ninja.)

I know that there is a path variable in Vim that to which I can add the other path. The last time I tried that it seemed less than convenient but maybe there's a good autocommand trick to fix that.

Currently I set my makeprg to a shell script that pipes build errors through a sed command.

2 Answers 2


Make and Ninja offer a -C flag. From Ninja:

-C DIR   change to DIR before doing anything else

If I run Ninja in a shell with the -C flag pointing somewhere that isn't the working directory, then the reported paths are the same as if I had cd'd over to the build directory. Nonetheless, Vim knows to become more intelligent when you use this flag in your makeprg and quickfix can then figure out where it should jump to. In the quickfix window, the reported paths are changed to be relative to Vim's working directory.


For example, if your ninja output directory is '$SRC/out'

:set makeprg=ninja\ -C\ $SRC/out

Note: the space should escape by backslash

  • Welcome to our site! It is always useful to link to the relevant help topic: :h 'makeprg' :)
    – statox
    Commented Apr 23, 2019 at 7:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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