created 2005 · complexity basic · author jheddings · version 6.0
Here is a newer solution that seems to alleviate any problems with multiple tabs and out of sync issues.
function! GetBufferList() redir =>buflist silent! ls redir END return buflist endfunction function! ToggleList(bufname, pfx) let buflist = GetBufferList() for bufnum in map(filter(split(buflist, '\n'), 'v:val =~ "'.a:bufname.'"'), 'str2nr(matchstr(v:val, "\\d\\+"))') if bufwinnr(bufnum) != -1 exec(a:pfx.'close') return endif endfor if a:pfx == 'l' && len(getloclist(0)) == 0 echohl ErrorMsg echo "Location List is Empty." return endif let winnr = winnr() exec(a:pfx.'open') if winnr() != winnr wincmd p endif endfunction nmap <silent> <leader>l :call ToggleList("Location List", 'l')<CR> nmap <silent> <leader>e :call ToggleList("Quickfix List", 'c')<CR>
Using this function and command:
command -bang -nargs=? QFix call QFixToggle(<bang>0) function! QFixToggle(forced) if exists("g:qfix_win") && a:forced == 0 cclose unlet g:qfix_win else copen 10 let g:qfix_win = bufnr("$") endif endfunction
Calling ':QFix' will "toggle" the quickfix open and closed. It's easiest to map this to something fast. I use:
nmap <silent> \` :QFix<CR>
If you want to force the window open, use ':QFix!' and the window will open or stay open.
Comments
Using autocommands, you can fix the "out-of-sync" issue. The entire code for this would be:
" toggles the quickfix window. command -bang -nargs=? QFix call QFixToggle(<bang>0) function! QFixToggle(forced) if exists("g:qfix_win") && a:forced == 0 cclose else execute "copen " . g:jah_Quickfix_Win_Height endif endfunction " used to track the quickfix window augroup QFixToggle autocmd! autocmd BufWinEnter quickfix let g:qfix_win = bufnr("$") autocmd BufWinLeave * if exists("g:qfix_win") && expand("<abuf>") == g:qfix_win | unlet! g:qfix_win | endif augroup END
I set g:jah_Quickfix_Win_Height
in my vimrc to something around 10.
Maybe I'm missing something, but why don't just use :copen
and :cclose
? Why do you need that function/command?
Because this is a toggle (open/close) in a single command, whereas :copen
and :cclose
are 2 different commands and 2 different mappings, in case you wish to map it.
You can use t:qfix_win
instead of g:qfix_win
if you need to switch between tabs.