Quickfix Location List linter window stays open - vim

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

Related

How to setup an autocommand for the preview window's buffer?

How do I setup an autocommand to trigger for the preview window's buffer solely? I have one that works on the quickfix window because its filetype is qf, so I can setup a FileType autocommand for it. I've checked quickfix window's buffer FileType but it's empty.
There's no explicit event, but you can easily check for the preview window via the 'previewwindow' option, and combine that with another event, e.g. WinEnter:
:autocmd WinEnter * if &previewwindow | ... | endif
The currently accepted answer didn't solve the problem for me because it only triggered when the preview window was entered, not when it was created.
Thanks to this vim tip, I found that BufWinEnter does fire on preview window creation.
autocmd! BufWinEnter * if &previewwindow | call s:previewWinSettings() | endif
The only times it won't fire are if using :pedit on an already-loaded buffer.

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

detect, if ctrlp is opened/active

I like the relativenumbers, but they turn out to be slow if I move through files with very long lines. So I added
autocmd CursorMoved,CursorMovedI * if &relativenumber | set norelativenumber | endif
autocmd CursorHold,CursorHoldI * set relativenumber
set updatetime=500
to my vimrc. It disables relative numbers during cursor movement and enables them afterwards again. However, this also enables them in ctrlp. Especially since ctrlp disables number, this causes columns to jump as soon as I cursor. Is there a way to detect, if I'm currently in the curlp file browser?
The CtrlP scratch buffer probably has a distinct name (if bufname('') =~ 'ctrlp')) or filetype (if &filetype ==# 'ctrlp') that you can use in a condition on a similar autocmd BufEnter.
(Note: I don't use the plugin, so the above names are just for illustration.)

Vim highlight a word with * without moving cursor

I would like to be able to highlight the word under cursor without going to the next result.
The solution I found uses a marker:
noremap * mP*N`P
Is there any better solution ?
I noticed vim is not really good to keep the position of its cursor after the execution of commands. For instance, when I switch buffer, the cursor go back to the begining of the line.
I might be interesting to have a global setting for this.
There's unfortunately no global setting for that.
One common solution to the "highlight without moving" problem is:
nnoremap * *``
One solution to the "keep the cursor position when switching buffers" problem is:
augroup CursorPosition
autocmd!
autocmd BufLeave * let b:winview = winsaveview()
autocmd BufEnter * if(exists('b:winview')) | call winrestview(b:winview) | endif
augroup END
As detailed here, you can define a map as follows:
:nnoremap <F8> :let #/='\<<C-R>=expand("<cword>")<CR>\>'<CR>:set hls<CR>
My SearchHighlighting plugin changes the * command, so that it doesn't jump to the next match; you can still jump by supplying a [count]. It also extends that command to visual mode (another frequently requested feature not in Vim), and some more.

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