Wikia

Vim Tips Wiki

Changes: Automatically quit Vim if quickfix window is the last

Edit

Back to page

(Change <tt> to <code>, perhaps also minor tweak.)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 31: Line 31:
 
:I don't think your concern is correct. The <code>quit!</code> 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).
 
:I don't think your concern is correct. The <code>quit!</code> 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 <code>!</code> needed? I think you have to go to a fair bit of trouble to make a change to a quickfix window, so a plain <code>quit</code> would suffice and be a tiny bit safer.
 
:Is the <code>!</code> needed? I think you have to go to a fair bit of trouble to make a change to a quickfix window, so a plain <code>quit</code> would suffice and be a tiny bit safer.
:Rather than use <code>winbufnr(2)</code>, I think the test could be {{tt|if winnr('$') < 2}}. [[User:JohnBeckett|JohnBeckett]] 07:00, May 10, 2011 (UTC)
+
:Rather than use <code>winbufnr(2)</code>, I think the test could be <code>if winnr('$') < 2</code>. [[User:JohnBeckett|JohnBeckett]] 07:00, May 10, 2011 (UTC)

Latest revision as of 12:32, July 15, 2012

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)

Around Wikia's network

Random Wiki