Fandom

Vim Tips Wiki

Automatically quit Vim if quickfix window is the last

1,624pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Tip 536 Printable Monobook Previous Next

created 2003 · complexity intermediate · author David Kalita · version 6.0


Usually I have open one window with source code and one with error codes (command :copen). When I quit the source code quickfix window stays open. The following causes the quickfix window to close automatically.

au BufEnter * call MyLastWindow()
function! MyLastWindow()
  " if the window is quickfix go on
  if &buftype=="quickfix"
    " if this window is last on screen quit without warning
    if winbufnr(2) == -1
      quit!
    endif
  endif
endfunction

CommentsEdit

The quit! should be changed to tabclose to be safe.

I don't think your concern is correct. The quit! would close the current buffer and discard any changes for that buffer. However, the command does not attempt to close any other buffer, so unsaved work in other tabs will be retained (Vim will not terminate).
Is the ! needed? I think you have to go to a fair bit of trouble to make a change to a quickfix window, so a plain quit would suffice and be a tiny bit safer.
Rather than use winbufnr(2), I think the test could be if winnr('$') < 2. JohnBeckett 07:00, May 10, 2011 (UTC)

Also on Fandom

Random Wiki