6

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)

Community
  • 1
  • 1
akavel
  • 4,789
  • 1
  • 35
  • 66

2 Answers2

5

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.

Community
  • 1
  • 1
akavel
  • 4,789
  • 1
  • 35
  • 66
  • Is it possible to populate the quickfix window with data from vim EX mode commads like :ls? Quickfix window with list of all open buffers? I tried `cexp ":execute ls"` but it doesnt work. Need some help with vimscripting – avimehenwal Dec 30 '20 at 14:13
4

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
akavel
  • 4,789
  • 1
  • 35
  • 66
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • Hm, I kinda found both of those, but they were both too long for me, only I didn't know how to express this; now seeing your answer I think I know: I'd prefer an answer of 1, max 2 lines... and most ideally something like `:! $RANDOM_COMMAND | :copen`... – akavel Aug 01 '14 at 15:19
  • hm, is it possible to type all your commands in one line, some kinda `:set makeprg=$FOOBAR ; :make ; :copen` or something? – akavel Aug 01 '14 at 15:23
  • 1
    Sure: `set makeprg=$FOOBAR|exe 'make'|copen`. – Ingo Karkat Aug 01 '14 at 17:46
  • Thanks a lot! Worked. I'll let myself edit your answer to add some more details I needed too. Also, I understand I can escape spaces only with a backslash? – akavel Aug 12 '14 at 08:58
  • Hm, given some [other answer](http://stackoverflow.com/a/6549828/98528), I think the `| exe 'make'| ` should work also when shortened to just `| make |`? or not? – akavel Aug 12 '14 at 09:13