In vim, how can I automatically maximize the help window - vim

When I display a vim helpfile by running e.g. :h au, the help is displayed in a horizontal split window:
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?

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

I assume you'd like the help window to be horizontally maximized.
Sorry, I can't reproduce your bug (tried on MacVim 7.4-258 and vim 7.4-258), but here are some suggestions:
Try changing wincmd _ to set winheight=9999 in your augroup script.
How about opening the help in a new tab with wincmd T instead?

I think you can achieve your goal of getting your help buffers to be maximized more simply by putting the following in your .vimrc (or wherever), instead of the augroup/autocmd:
:set helpheight=9999

|CTRL-W_o| CTRL-W o close all but current window (like |:only|)

Related

Vdebug - maximize DebuggerWatch window on load

The vim plugin vdebug opens several splits when it starts the debugger. One of those is DebuggerWatch at the top right. I read somewhere I can set an autocmd to listen for that buffer opening and take action. In this case I want to run :resize 100 to maximize the height. I know how to set an autocmd by filetype or by file glob, but not by a buffer name that is not a file. How do I write an autocmd for this?
I was able to resize on entering the buffer like this:
augroup vdebugwatchpanellarger
autocmd!
autocmd BufEnter DebuggerWatch resize 999
autocmd BufEnter DebuggerStack resize 999
augroup END
This is helpful because at least if I navigate to one of these buffers it will resize them. But I haven't been able to get it resizing on loading the buffer yet. I tried BufRead, BufNew, BufAdd instead of BufEnter. No effect.
The plugin sets the :help 'syntax' option of the "DebuggerWatch" window to debugger_watch so using the :help Syntax event should work:
augroup vdebugwatchpanellarger
autocmd!
autocmd Syntax debugger_watch resize 999
augroup END

Quickfix Location List linter window stays open

I love the linters and the quickfix window, but on occasion I don't want to fix anything and simply want to exit the file. The current issue is that when I close the file, the quickfix window stays open. I know I can use :qa to exit both at the same time, but I often forget to.
I spent a few hours trying to figure out how to close the quickfix buffer if it was the only open window/buffer left, but had no luck.
Anyone else know how to handle this better?
That feature is part of my vim-qf plugin but you don't really need a full-fledged plugin for such a mundane task. Actually, there have been many snippets floating around the internet for many years. Here is one:
augroup MyGroup
autocmd!
if exists('##QuitPre')
autocmd QuitPre * if &filetype != 'qf' | silent! lclose | endif
endif
augroup END
Basically, it closes the current quickfix/location window if it is the last remaining window in the current tab page.
Reference:
:help :augroup
:help :autocmd
:help exists()
:help QuitPre
if version >= 700
" automatically close quickfix if it's the only window left
autocmd WinEnter * if winnr('$') == 1 && &buftype == "quickfix" | quit | endif
endif

vimrc: how to auto change window focus when vim is loaded

I've let tagbar and NERDtree autoload when vim is opening any file.
The window layout from left to right is:
NERDTree----My source code----TagBar
The problem is, each time vim is up, the NERDtree on the left gets the focus(keyboard). I wish to make the middle window(My source code) having the focus, so I can start coding immediately. Or else I have to C-w l to switch windows each time.
How to set this in ~/.vimrc?
Thanks a lot.
The plugins probably use :autocmd VimEnter to open their sidebars. You can define a similar autocmd, but it has to run after the plugins'.
Put the following into ~/.vim/after/plugin/middleWindow.vim:
autocmd VimEnter * 2wincmd w
This goes to the second available window.
It's a bit tricky, because I don't know when in your start-up sequence, all three buffers are loaded. If you use Vim8, you can do the wincmd on a timer. This works for me:
call timer_start(100, { -> execute( "wincmd l") })

VIM: Open separate help/hint/text file automatically in a vsplit

I have the following problem, or lets say idea, with vim. When I am writing latex documents I want automatically open a file ~/.vim/latex_hints, where I collected some hints, shortcuts, workarounds,..., in vsplit on the right side. The hint file should be loaded read only and automatically closing when I close the latex document.
After a few experiments I added the following commands to my vimrc:
function Handletexfile()
setlocal cc=80
setlocal wrap
setlocal textwidth=80
belowright vsplit +setl\ ro\ nomodifiable ~/.vim/latex_hints
endfunction
autocmd BufRead,BufNewFile *.tex call Handletexfile()
and
function Handletexfileexit()
let tablist = []
call extend(tablist, tabpagebuflist(tabpagenr()))
for b in tablist
echo b . " ". bufname(b)
if bufname(b) =~ "vim/.*_hints"
echo "Close buffer..". b
execute "bdelete! ".b
endif
endfor
endfunction
autocmd BufWinLeave *.* call Handletexfileexit()
When I open a tex file, my hint file is displayed on the right side as read only and not modifiable. But when I close using :q or :wq the buffers open in the current tab are listed and the one matching to the hint file is selected by the if statement. But I get the following output
1 abstract.tex
2 ~/.vim/latex_hints
Close buffer..2
and my vim crashes with an segfault.
The first part of your requirement translates pretty straightforward into Vimscript:
autocmd BufWinEnter <buffer> belowright vsplit +setl\ ro ~/.vim/latex_hints
Put this into ~/.vim/after/ftplugin/tex.vim, or prepend :autocmd FileType tex to the above command.
The latter part is more complex; on BufWinLeave, you'd have to check all other windows for the opened cheat file with bufwinnr(), go to it (:wincmd w), and :close it.
As an alternative use the preview window with your hint file. The preview window gives you some advantages:
It is small and out of the way
Can close it easily via <c-w>z from any other window
Can jump to the preview window from any window easily via <c-w>P (jump back via <c-w>p)
You can do this via the :pedit command. For example:
autocmd BufWinEnter <buffer> pedit +setl\ ro ~/.vim/latex_hints
Personally I feel like it would be better to create command or mapping to open your hints files as you may eventually out grow the file as time goes on. I would also set the 'bufhidden' to wipe and un-list the buffer with 'nobuflisted'.
You may also want to look into getting a nice snippet plugin.

Quickfix & Preview window resizing

Normally when quickfix window opens it changes the screen layout, but Vim restores
it when that window is closed.
But there is a situation where the layout restoration fails: when the
preview window is open, vertical splits are presents and :wincmd J is executed
in quickfix (or it is opened with :botright copen). In this case the size of
preview window is changed.
I came with a solution which I placed on ~/.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
, but I was wondering if there is some better/simpler solutions to this
problem.
If you can reproduce the problem in plain vanilla Vim (vim -N -u NONE), I'd report it to the vim_dev mailing list to have it fixed inside Vim. The preview window should not change its size when other, normal windows could stand in for it.
If this is just a peculiarity of your setup, I think your implemented workaround is fine; I would probably solve it along the same lines.
I was with this problem and I've tried your proposed qf.vim but it didn't work. I found something that did, in the qf help page, =|, so put this in your .vimrc:
au FileType qf botright cwindow
I used your answer to improve the default auto resize behaviour of vim.
It's not really an answer to this question but hopefully other people might find it useful as I stumbled across this question for that reason:
nmap <silent> <C-w>= :call ResizeAllWindows()<cr>
function! ResizeAllWindows()
call RestorePreviewWindowHeight()
wincmd = "set all equal after restore
endfunction
function! RestorePreviewWindowHeight()
silent! wincmd P "jump to preview, but don't show error
if &previewwindow
exec "resize" &previewheight
wincmd p "jump back
endif
endfunction

Resources