Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim redirect output to quickfix

Tags:

vim

macvim

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 output would go into the quickfix window

like image 468
tawheed Avatar asked Aug 07 '13 23:08

tawheed


3 Answers

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.

like image 151
brianmearns Avatar answered Nov 10 '22 08:11

brianmearns


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
like image 36
Ingo Karkat Avatar answered Nov 10 '22 10:11

Ingo Karkat


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.

like image 6
Jonathan Avatar answered Nov 10 '22 09:11

Jonathan