vim redirect output to quickfix

后端 未结 4 2051
孤城傲影
孤城傲影 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:31

    I mapped leader + j + r to run my java code and display it in the quickfix window by doing

    map <leader>jr :set makeprg=java <CR>:make %:r<CR>:copen<CR>
    
    0 讨论(0)
  • 2021-02-13 10:35

    Please note that the quickfix window is for specific output (e.g. of compiler or syntax checker tools) which includes references (i.e. line and column numbers) to the current buffer. There's a lot of infrastructure around this: 'makeprg', 'errorformat', etc., usually bundled into a compiler plugin.

    Though you can redirect arbitrary output into the quickfix window, it provides little benefit (and has the downside of clobbering 'makeprg') over reading the output of an external program into a new scratch buffer, e.g. like this:

    :new|0read !java #:r
    
    0 讨论(0)
  • 2021-02-13 10:45

    Try this:

    1. set makeprg=java
    2. make %:r

    It's a bit of hack, and of course assumes you aren't already using makeprg for your actual build script.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题