vim redirect output to quickfix

后端 未结 4 2052
孤城傲影
孤城傲影 2021-02-13 10:20

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

4条回答
  •  执念已碎
    2021-02-13 10:55

    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.

    Option 1: Using makeprg and compiler plugins

    I 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.

    Option 2: Create a command to do it

    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.

提交回复
热议问题