vimvi

How to create a key map to open and close the quickfix window in Vim


If I use :lopen, Vim opens the quickfix window, and if I use :lcl on the window with errors (or the quickfix window itself), it closes it.

What I want to do in my .vimrc is to create a map that opens the quickfix like this:

nnoremap <F2> :lopen 10<CR>

but when I press F2 again it closes it using :lcl.

Is there a way to know if the quickfix window is open and then execute the :lcl?


Solution

  • In sufficiently new versions of vim (where getwininfo is available), try:

    function! ToggleQuickFix()
        if empty(filter(getwininfo(), 'v:val.quickfix'))
            copen
        else
            cclose
        endif
    endfunction
    
    nnoremap <silent> <F2> :call ToggleQuickFix()<cr>
    

    Customizations,