Is it possible to redirect an output of a command to the quick fix window?
The command I am running is
:!java %:r
and was hoping the o
I would suggest one of two options: configure makeprg
to run java like you want, or create a mapping or command to populate the quickfix list without changing anything else.
makeprg
and compiler pluginsI would generally set the makeprg
option for this, as others have said. It's not a hack, that's exactly what the makeprg
option is for.
The only problem is if you have another build script you want to run as well. A more general solution is to create a simple compiler plugin. For instance, somewhere on your runtimepath
, you can create a file under compiler/java.vim
and set it to something like this:
if exists("current_compiler")
finish
endif
let current_compiler = "java"
CompilerSet makeprg=java
Now when you're working with java, you can do :compiler java
and then your makeprg
will be set as desired in the current window. If you want to use it for all windows, use :compiler! java
, with a bang. Not all compiler plugins set the makeprg
option, but you can always reset it with :set makeprg&
. Try :help write-compiler-plugin
for more information.
Alternatively, you can also use cexpr
to populate the quickfix list. For instance:
:cexpr system('java ' . shellescape(expand('%:r')))
The expand
is necessary to expand the '%:r'
in an expression, and shellescape
escapes it so it can be used as an argument to a shell command. Then the string 'java '
is prepended to the escaped path and the result is invoked as a shell command by system
. The output from this command is used to load the quickfix list.
The nice thing about this is that it doesn't change makeprg
or any other settings, but still lets you easily populate the quickfix list. Of course, you'll probably want to map this or define a custom command for it.