Copy from Quickfix to the Args list


Vim has several distinct lists: the quickfix list and the args list. Each have their own primary use cases but sometimes you might want the contents of your quickfix list copied into your args list. Why?

We recently wrote about how to open all files in a directory in Vim at the same time. First, you populate your args list with the directory contents and then you open everything in your args list.

A similar tactic can be applied for opening the results of a search that inhabits your quickfix list. Let’s say you’ve searched for something with :grep and now your quickfix list is populated with a list of 5 files that match that result. You might find it convenient to look at all of those files at the same time, each in a split.

To do so, we need to copy the quickfix list into our args list first. I use a snippet like the one below inside my .vimrc (which comes from Drew Neil, author of Practical Vim):

command! -nargs=0 -bar Qargs execute 'args' QuickfixFilenames()
function! QuickfixFilenames()
  let buffer_numbers = {}
  for quickfix_item in getqflist()
    let buffer_numbers[quickfix_item['bufnr']] = bufname(quickfix_item['bufnr'])
  return join(map(values(buffer_numbers), 'fnameescape(v:val)'))

This gives you a command called :Qargs which copies the items from your quickfix list into your args list. (You can also use the qargs plugin for this.) From there, it’s just a matter of running :sall to open all the resulting files. Let’s watch this in action:

In the gif, I first do a :Ggrep TODO to find all the references to TODO in my app. The resulting list is populated into the quickfix list. Then I use our handy new command :Qargs to copy that into the args list. Then I use :sall to open all those files in splits. (Try :vertical sall, too!)

For more, try :help args and :help quickfix.

How useful was this tip?

Average rating 2.7 / 5. Vote count: 305

No votes so far! Be the first to rate this tip.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Written by

Colin Bartlett

105 Posts

Vim enthusiast and software developer for more than 20 years.
View all posts