通常当 quickfix 窗口打开时它会改变屏幕布局,但是当窗口关闭时 Vim 会恢复它。
但是有一种情况是布局恢复失败:预览窗口打开时出现垂直分割,:wincmd J
在quickfix中执行(或者用 开启:botright copen
)。在这种情况下,预览窗口的大小会改变。
我提出了一个解决方案,我把它放在 ~/.vim/ftplugin/qf.vim 上,
" Only do this when not done yet for this buffer
if exists("b:did_ftplugin")
finish
endif
" expand quickfix when there are vertical splits
wincmd J
func! RestorePreviewWindow()
let l:quickfixHeight = winheight(0)
wincmd p " include previous window on jump list
silent! wincmd P " jump to preview window
if &previewwindow " if we really get there...
exe "resize " . (&previewheight - l:quickfixHeight - 1)
wincmd p " back to old window
endif
endfunc
augroup quickfixClosing
au!
au BufDelete <buffer> call RestorePreviewWindow()
augroup END
,但我想知道这个问题是否有更好/更简单的解决方案。