Search code examples
vimautocmd

In vim, how can I automatically maximize the help window


When I display a vim helpfile by running e.g. :h au, the help is displayed in a horizontal split window:

vim help

Currently I always run Ctrl+w _ but I would prefer the help buffer to open in a maximized window automatically.

I've tried to create an autocmd to solve the issue:

"Automatically maximize help buffers
augroup filetype_help
  autocmd!
  autocmd BufWinEnter,FileType help wincmd _
augroup END

which only works sporadically.

EDIT:

I have done some further debugging.
Opening a certain help page the first time, e.g. :h au displays it maxmimized when having above augroup in my .vimrc.

Closing the helpfiles window via :q and then reopening the same helpfile a second time causes the help file to be displayed in a split as in the screenshot above.

Closing the helpfiles buffer window via :bd and then reopening it, causes it to being displayed maximized as desired.

How can I rewrite my augroup so that it also maximizes an already opened help buffer?


Solution

  • The BufWinEnter event matches the help filename, so the help pattern (which is fine for a FileType match) won't work. The 'filetype' option is only set once for a buffer, so when it is reused (after :q, but not after :bd), your maximization fails, in the way you've reported.

    Instead, have the :autocmd match all buffers, and check for the 'buftype':

    augroup filetype_help
        autocmd!
        autocmd BufWinEnter * if &l:buftype ==# 'help' | wincmd _ | endif
    augroup END