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
?
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,
vertical copen
copen
/cclose
with lopen
/lclose
, and v:val.quickfix
with v:val.loclist
.)There is a vim plugin: https://github.com/milkypostman/vim-togglelist
Here is another way to do it, probably skipping some gory details but it works:
function! ToggleQuickFix()
if exists("g:qwindow")
lclose
unlet g:qwindow
else
try
lopen 10
let g:qwindow = 1
catch
echo "No Errors found!"
endtry
endif
endfunction
nmap <script> <silent> <F2> :call ToggleQuickFix()<CR>
If there are no errors the lopen will not work so I try catch that, in case there is it opens the window and creates a variable. then if it doesn't it just closes it.
The cool thing is that this approach can be used to everything you would like to toggle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With