Vim Tips Wiki
Register
(Change to TipImported template + severe manual clean)
(Change <tt> to <code>, perhaps also minor tweak.)
 
(7 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=536
 
|id=536
 
|previous=535
 
|previous=535
 
|next=537
 
|next=537
|created=August 21, 2003
+
|created=2003
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=David Kalita
 
|author=David Kalita
 
|version=6.0
 
|version=6.0
 
|rating=11/5
 
|rating=11/5
  +
|category1=Compiler
  +
|category2=Searching
 
}}
 
}}
Usually I have open one window with source code and one with error codes (command <tt>:copen</tt>). When I quit the source code quickfix window stays open. The following causes the quickfix window to close automatically.
+
Usually I have open one window with source code and one with error codes (command <code>:copen</code>). When I quit the source code quickfix window stays open. The following causes the quickfix window to close automatically.
   
 
<pre>
 
<pre>
Line 16: Line 17:
 
function! MyLastWindow()
 
function! MyLastWindow()
 
" if the window is quickfix go on
 
" if the window is quickfix go on
if &amp;buftype=="quickfix"
+
if &buftype=="quickfix"
 
" if this window is last on screen quit without warning
 
" if this window is last on screen quit without warning
 
if winbufnr(2) == -1
 
if winbufnr(2) == -1
Line 27: Line 28:
 
==Comments==
 
==Comments==
   
  +
The quit! should be changed to tabclose to be safe.
----
 
  +
: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).
[[Category:Compiler]]
 
  +
: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 <code>if winnr('$') < 2</code>. [[User:JohnBeckett|JohnBeckett]] 07:00, May 10, 2011 (UTC)

Latest revision as of 12:32, 15 July 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

Comments[]

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)