Vim Tips Wiki
Advertisement

Previous TipNext Tip

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

Created: October 18, 2004 10:24 Complexity: intermediate Author: Ton van den Heuvel Version: 6.0 Karma: 4/1 Imported from: Tip#806

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


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

Comments

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:29


Sorry for the clutter, I'm new here :) I hope an admin could properly format the code and remove these notes please.

Anonymous , October 18, 2004 10:33


Shouldn't you be save/restoring those global values:

" Redraw after executing the function. 
set lazyredraw 
" Ignore WinEnter events for now. 
set ei=WinEnter 


Anonymous , October 21, 2004 6:50


Advertisement