將行從快速修復視窗移動到單獨的快速修復視窗

將行從快速修復視窗移動到單獨的快速修復視窗

我使用快速修復視窗來聚合來自 的搜尋結果grep。然而有時我希望能夠回傳一些結果。有沒有辦法創建另一個快速修復窗口,我可以在其中貼上這些行,以便以後可以引用它們?

謝謝

答案1

Vim 附帶:help cfilter-plugin這讓你消除快速修復您不再需要的條目,因此它的工作方式相反。

或者,您可以基於當前的快速修復列表建立新的快速修復列表,因為 Vim 維護了一系列這些列表(cp.stack)。:help :colder)。但是,如果您想以互動方式選擇多個條目,那麼第一次建立新清單並在接下來的時間呼叫該新清單是很麻煩的。

所以你可以巧妙地使用location-list反而;它的工作方式與 QuickFix 類似,但它是本機視窗(此處:QuickFix 視窗)。

要複製整個當前條目而不重新解析它,低級getqflist()setloclist()可以使用:

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

nnoremap <LocalLeader>ql您可以透過在前面新增並將映射定義放入 中,將其綁定到鍵~/.vim/after/ftplugin/qf.vim

插件解決方案

這是一個完整的插件,還支援計數、重複via .、視覺模式以及將位置清單條目收集到快速修復清單中。它需要我的ingo 庫插件,1.040 或更高版本,目前僅作為開發快照提供GitHub。也可以複製到~/.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

相關內容