sponsor Vim development Vim logo Vim Book Ad

intermediate Tip #806: Always keep quickfix window at specified height

 tip karma   Rating 4/1, Viewed by 1239 

created:   October 18, 2004 10:24      complexity:   intermediate
author:   Ton van den Heuvel      as of Vim:   6.0

When I use Vim I have multiple windows open which are all maximized after becoming active, see for example vimtip #173

This makes debugging code in combination with the quickfix window a little difficult. After switching from the quickfix window to the code, the quickfix window is minimized and the error message is out of sight. Put the following in your .gvimrc to keep the quickfix window at a user specified height:


" Maximize the window after entering it, be sure to keep the quickfix window
" at the specified height.
au WinEnter * call MaximizeAndResizeQuickfix(8)

" Maximize current window and set the quickfix window to the specified height.
function MaximizeAndResizeQuickfix(quickfixHeight)
" Redraw after executing the function.
set lazyredraw

" Ignore WinEnter events for now.
set ei=WinEnter

" Maximize current window.
wincmd _

" If the current window is the quickfix window
if (getbufvar(winbufnr(winnr()), "&buftype;") == "quickfix")
" Maximize previous window, and resize the quickfix window to the
" specified height.
wincmd p
resize
wincmd p
exe "resize " . a:quickfixHeight
else
" Current window isn't the quickfix window, loop over all windows to
" find it (if it exists...)
let i = 1
let currBufNr = winbufnr(i)
while (currBufNr != -1)
" If the buffer in window i is the quickfix buffer.
if (getbufvar(currBufNr, "&buftype;") == "quickfix")
" Go to the quickfix window, set height to quickfixHeight, and jump to the previous
" window.
exe i . "wincmd w"
exe "resize " . a:quickfixHeight
wincmd p
break
endif
let i = i + 1
let currBufNr = winbufnr(i)
endwhile
endif
set ei-=WinEnter
set nolazyredraw
endfunction


As an added bonus; map ,m to the make command and let the quickfix window pop up if there were any errors:


" Remap ,m to make and open error window if there are any errors. If there
" weren't any errors, the current window is maximized.
map <silent> ,m :mak<CR><CR>:cw<CR>:call MaximizeIfNotQuickfix()<CR>

" Maximizes the current window if it is not the quickfix window.
function MaximizeIfNotQuickfix()
if (getbufvar(winbufnr(winnr()), "&buftype;") != "quickfix")
wincmd _
endif
endfunction

 rate this tip  Life Changing Helpful Unfulfilling 

<< Windows: gvim as an external editor | Backing up and commenting .vimrc >>

Additional Notes

Anonymous, October 18, 2004 10:29
Hmmm, something went wrong with formatting, here it is again, properly formatted now (I hope...)

" Maximize the window after entering it, be sure to keep the quickfix window
" at the specified height.
au WinEnter * call MaximizeAndResizeQuickfix(8)

" Maximize current window and set the quickfix window to the specified height.
function MaximizeAndResizeQuickfix(quickfixHeight)
        " Redraw after executing the function.
        set lazyredraw

        " Ignore WinEnter events for now.
        set ei=WinEnter

        " Maximize current window.
        wincmd _

        " If the current window is the quickfix window
        if (getbufvar(winbufnr(winnr()), "&buftype;") == "quickfix")
        " Maximize previous window, and resize the quickfix window to the
        " specified height.
        wincmd p
        resize
        wincmd p
        exe "resize " . a:quickfixHeight
        else
        " Current window isn't the quickfix window, loop over all windows to
        " find it (if it exists...)
        let i = 1
        let currBufNr = winbufnr(i)
        while (currBufNr != -1)
        " If the buffer in window i is the quickfix buffer.
        if (getbufvar(currBufNr, "&buftype;") == "quickfix")
        " Go to the quickfix window, set height to quickfixHeight, and jump to the previous
        " window.
        exe i . "wincmd w"
        exe "resize " . a:quickfixHeight
        wincmd p
        break
        endif
        let i = i + 1
        let currBufNr = winbufnr(i)
        endwhile
        endif
        set ei-=WinEnter
        set nolazyredraw
endfunction

" Remap ,m to make and open error window if there are any errors. If there
" weren't any errors, the current window is maximized.
map <silent> ,m :mak<CR><CR>:cw<CR>:call MaximizeIfNotQuickfix()<CR>

" Maximizes the current window if it is not the quickfix window.
function MaximizeIfNotQuickfix()
        if (getbufvar(winbufnr(winnr()), "&buftype;") != "quickfix")
        wincmd _
        endif
endfunction
Anonymous, October 18, 2004 10:33
Sorry for the clutter, I'm new here :) I hope an admin could properly format the code and remove these notes please.
Anonymous, October 21, 2004 6:50
Shouldn't you be save/restoring those global values:

        " Redraw after executing the function.
        set lazyredraw

        " Ignore WinEnter events for now.
        set ei=WinEnter
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
    stats
Sponsored by Web Concept Group Inc. SourceForge.net Logo