Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I browse output of any random command in quickfix window?

Tags:

vim

I'd like to run some $RANDOM_COMMAND, and have the results opened in ("piped to") the quickfix window (:copen/:cfile). Is this possible, without having to define some commands in vimrc? (Hopefully in some "simple way", i.e. I'd like to be able to memorize this so I can run this on any new random box with vanilla vim that I'll have to login to.)

edit: initially didn't know how to express "simple way" more precisely, but now I know at least partially: I'd much prefer an answer of 1, max 2 lines.

edit2: tried something like below (from this and this):

:call setqflist(split(system('RANDOM_COMMAND'), '\n'))
:copen

but didn't seem to work anyway :/ (and mucho ugly too)

like image 800
akavel Avatar asked Aug 01 '14 15:08

akavel


2 Answers

One way to do this:

:set makeprg=$RANDOM_COMMAND
:make
:copen

Or, execute the command and capture the output in a temporary file:

:! $RANDOM_COMMAND > out
:cfile out
:copen

In any way, the output must match with the 'errorformat' setting, so that Vim can parse the file name and line numbers (if you need those; but otherwise, you could just use a scratch buffer as well the quickfix list).

[edit] Some improvements

To make this a oneliner, then somewhat shortened, you can:

:set mp=RANDOM_COMMAND | make | copen

Whitespaces in command must be escaped with backslash; also, the make command can take arguments, which get expanded in place of a $*; a more full-blown example thus:

:set mp=mycommand\ -d\ $PWD\ $* | make myarg | copen

Alternatively, similar thing can be done with :set grepprg and :grep, giving even shorter line:

:set gp=mycommand\ -d\ $PWD\ $* | gr myarg | copen
like image 95
Ingo Karkat Avatar answered Oct 02 '22 05:10

Ingo Karkat


Hmh, found the simplest solution in the end, by reading through the regular vimdoc for quickfix window:

:cex system('$RANDOM_COMMAND') | copen

(the | copen part is optional).

Still, Ingo Karkat's solution can have usability advantage, as on consecutive runs it's enough to run shorter :grep there.

like image 22
akavel Avatar answered Oct 02 '22 05:10

akavel