0

I use quickfix window for aggregating search results from grep. However sometimes I want to be able to get back at some of the results. Is there a way to create another quickfix window where I could paste the lines and so I could reference them later?

Thanks

1 Answer 1

0

Vim ships with the :help cfilter-plugin that lets you remove quickfix entries you don't want any longer, so it works the other way around.

Alternatively, you could either create a new quickfix list based on the current one, as Vim maintains a stack of those (cp. :help :colder). However, if you want to interactively select multiple entries, that back and forth, creation of a new list the first time and recall of that newer list the following times is cumbersome.

So you could cleverly use the location-list instead; it works just like quickfix, but is local to a window (here: the quickfix window).

To copy the whole current entry without re-parsing it, the low-level getqflist() and setloclist() can be used:

:call setloclist(0, [getqflist()[line('.') - 1]], 'a')
      |              |           |                 |
      |              |           |                 +-- ... and append
      |              |           +-- select the current one
      |              +-- entries from the quickfix list
      +-- Location list for the current window ...

You could bind that to a key by prepending nnoremap <LocalLeader>ql, and putting the mapping definition in ~/.vim/after/ftplugin/qf.vim.

Plugin solution

Here's a complete plugin that also support count, repeat via ., visual mode, and collecting location list entries into the quickfix list. It requires my ingo-library plugin, 1.040 or higher, currently only available as a dev snapshot on GitHub. It can also be copied into ~/.vim/after/ftplugin/qf.vim:

function! s:Mapping( mappingType, sourceType, targetType, startLnum, endLnum ) abort
    if ingo#window#quickfix#IsQuickfixList(1) != a:sourceType
        call ingo#err#Set(printf('Not in a %s list', (a:sourceType == 1 ? 'quickfix' : 'location')))
        return 0
    endif

    let l:qfEntries = ingo#window#quickfix#GetList()[(a:startLnum - 1) : (a:endLnum - 1)]
    if empty(l:qfEntries)
        call ingo#err#Set('No entries')
        return 0
    endif

    silent call ingo#event#Trigger(printf('QuickFixCmdPre %scopy', ingo#window#quickfix#GetPrefix(a:targetType)))
        call ingo#window#quickfix#SetOtherList(a:targetType, l:qfEntries, 'a')
    silent call ingo#event#Trigger(printf('QuickFixCmdPost %scopy', ingo#window#quickfix#GetPrefix(a:targetType)))
    echomsg printf('%s entr%s copied to %s', len(l:qfEntries), (len(l:qfEntries) == 1 ? 'y' : 'ies'), ingo#window#quickfix#GetName(a:targetType))

    let l:mappingPrefix = "\<Plug>(QuickfixCopyTo" . a:mappingType
    silent! call       repeat#set(l:mappingPrefix . 'Lines)', len(l:qfEntries))
    silent! call visualrepeat#set(l:mappingPrefix . 'Selection)', len(l:qfEntries))

    return 1
endfunction

" [N]<LocalLeader>qc    Copy the current [N] location list entries to the
"           quickfix list.
" [N]<LocalLeader>ql    Copy the current [N] quickfix entries to a location list
"           (for the current quickfix window).
nnoremap <buffer> <silent> <Plug>(QuickfixCopyToQuickfixLines)      :<C-u>if ! <SID>Mapping('Quickfix', 2, 1, line('.'), line('.') + v:count1 - 1)<Bar>echoerr ingo#err#Get()<Bar>endif<CR>
xnoremap <buffer> <silent> <Plug>(QuickfixCopyToQuickfixSelection)  :<C-u>if ! <SID>Mapping('Quickfix', 2, 1, line("'<"), line("'>"))<Bar>echoerr ingo#err#Get()<Bar>endif<CR>
nnoremap <buffer> <silent> <Plug>(QuickfixCopyToLocListLines)       :<C-u>if ! <SID>Mapping('LocList',  1, 2, line('.'), line('.') + v:count1 - 1)<Bar>echoerr ingo#err#Get()<Bar>endif<CR>
xnoremap <buffer> <silent> <Plug>(QuickfixCopyToLocListSelection)   :<C-u>if ! <SID>Mapping('LocList',  1, 2, line("'<"), line("'>"))<Bar>echoerr ingo#err#Get()<Bar>endif<CR>
if ! hasmapto('<Plug>(QuickfixCopyToQuickfixLines)', 'n')
    nmap <LocalLeader>qc <Plug>(QuickfixCopyToQuickfixLines)
endif
if ! hasmapto('<Plug>(QuickfixCopyToQuickfixSelection)', 'v')
    xmap <LocalLeader>qc <Plug>(QuickfixCopyToQuickfixSelection)
endif
if ! hasmapto('<Plug>(QuickfixCopyToLocListLines)', 'n')
    nmap <LocalLeader>ql <Plug>(QuickfixCopyToLocListLines)
endif
if ! hasmapto('<Plug>(QuickfixCopyToLocListSelection)', 'v')
    xmap <LocalLeader>ql <Plug>(QuickfixCopyToLocListSelection)
endif
1
  • Wow thanks for exhausting answer! I'll be sure to check it out. Oct 23, 2019 at 8:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .