It is often the case that I am in projects with varying filetypes. Sometimes I'm compiling using :make<CR> and sometimes I'm using plugins like ALE. At any given time, I probably only have one helper window (location-list or quickfix) at the bottom of my window. However, different tools choose to use either output window mapping. This annoys me, since my brain can only focus on one set of errors at a time anyway.

How can I make it so that regardless of whether I happen to have the location-list or quickfix window open, my keyboard shortcuts just "do the right thing"? I want to map <F10> to :lnext or :cnext and <F9> to :lprev or :cprev, depending on the context of the window.

  • 1
    Probably you may use lmake command? It is same as ":make", except the location list for the current window is used instead of the quickfix list. And then just do lnext and lprev always.
    – Dmitry
    Commented Jun 14, 2020 at 20:32
  • That's an interesting idea, this also applies to non-make tools, like Fzf and/or vimgrep, though, so I feel like a solution at the later stage of the pipeline might end-up working a bit more effectively and catch more scenarios/tools. Commented Jun 14, 2020 at 21:14

1 Answer 1

nnoremap <silent><F9> :call <SID>qfnext(v:false)<CR>
nnoremap <silent><F10> :call <SID>qfnext(v:true)<CR>

function! s:qfnext(next) abort
    " find all 'quickfix'-type windows on the current tab
    let qfwin = filter(getwininfo(), {_, v -> v.quickfix && v.tabnr == tabpagenr()})
    if !empty(qfwin)
        " using the first one found
        if qfwin[0].winid == getqflist({'nr': 0, 'winid': 0}).winid
            " it's quickfix
            execute a:next ? 'cnext' : 'cprev'
            " assume it's loclist
            " must execute it in the host window or in loclist itself
            call win_execute(qfwin[0].winid, a:next ? 'lnext' : 'lprev', '')
  • I added a fallback to lnext/lprev at the end in the case that there were no quickfix windows open, because sometimes the location-list is not visible. Thanks! Commented Jun 14, 2020 at 21:51
  • @WillBradley would you mind posting your version?
    – jav
    Commented May 17, 2021 at 17:00
  • Place "execute a:next ? 'lnext' : 'lprev'" on a new line after the final endif. Note that this tends to work sometimes, but sadly not always. Commented May 18, 2021 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.