Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Since it is not allowed to affect the buffer list with a <code>:bufdo</code>-argument command (see <code>:help :bufdo</code>), we have to use more wordy yet fairly straightforward Vim script.</p> <p>The function below enumerates all existing buffer numbers and deletes those that do not have a name (displayed as <code>[No Name]</code> in the interface) and any unsaved changes. (The latter is guaranteed by the <code>:bdelete</code> command which, without the <code>!</code> sign, does not delete a modified buffer).</p> <pre><code>function! DeleteEmptyBuffers() let [i, n; empty] = [1, bufnr('$')] while i &lt;= n if bufexists(i) &amp;&amp; bufname(i) == '' call add(empty, i) endif let i += 1 endwhile if len(empty) &gt; 0 exe 'bdelete' join(empty) endif endfunction </code></pre> <p>If you would like to delete empty buffers completely, including unloaded ones, consider (with care!) replacing <code>bdelete</code> with <code>bwipeout</code> (see <code>:help :bd</code>, <code>:help :bw</code>).</p> <p>To test the contents of a buffer to delete, use <code>getbufline()</code> function. For example, to be absolutely sure that buffer contains no text in it, modify the <code>if</code> statement inside the <code>while</code> loop as follows.</p> <pre><code>if bufloaded(i) &amp;&amp; bufname(i) == '' &amp;&amp; getbufline(i, 1, 2) == [''] </code></pre> <p>Note that <code>bufexists()</code> is changed to <code>bufloaded()</code> here. It is necessary because it is possible to get the contents only of those buffers that are loaded (for unloaded buffers <code>getbufline()</code> returns empty list regardless of their contents).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload