Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 922193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:59:23+00:00 2026-05-15T18:59:23+00:00

How can I pare down my buffer list to only include buffers that are

  • 0

How can I pare down my buffer list to only include buffers that are currently open in a window/tab?

When I’ve been running Vim for a long time, the list of buffers revealed by the :ls command is too large to work with. Ideally, I would like to delete all of the buffers which are not currently visible in a tab or window by running a custom command such as :Only. Can anybody suggest how to achieve this?

It looks like the :bdelete command can accept a list of buffer numbers, but I’m not sure how to translate the output from :ls to a format that can be consumed by the :bdelete command. Any help would be appreciated.

Clarification

Lets say that in my Vim session I have opened 4 files. The :ls command outputs:

:ls
1  a   "abc.c"
2  h   "123.c"
3  h   "xyz.c"
4  a   "abc.h"

Buffer 1 is in the current tab, and and buffer 4 is in a separate tab, but
buffers 2 and 3 are both hidden. I would like to run the command :Only, and
it would wipe buffers 2 and 3, so the :ls command would output:

:ls
1  a   "abc.c"
4  a   "abc.h"

This example doesn’t make the proposed :Only command look very useful, but
if you have a list of 40 buffers it would be very welcome.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:59:23+00:00Added an answer on May 15, 2026 at 6:59 pm

    I’ve adapted Laurence Gonslaves solution.

    command! -nargs=* Only call CloseHiddenBuffers()
    function! CloseHiddenBuffers()
      " figure out which buffers are visible in any tab
      let visible = {}
      for t in range(1, tabpagenr('$'))
        for b in tabpagebuflist(t)
          let visible[b] = 1
        endfor
      endfor
      " close any buffer that are loaded and not visible
      let l:tally = 0
      for b in range(1, bufnr('$'))
        if bufloaded(b) && !has_key(visible, b)
          let l:tally += 1
          exe 'bw ' . b
        endif
      endfor
      echon "Deleted " . l:tally . " buffers"
    endfun
    

    I changed it to use bwipeout instead of bdelete, and added the message to show how many buffers have been removed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can a LINQ enabled app run on a machine that only has the .NET
Can somebody point me to a resource that explains how to go about having
Can you cast a List<int> to List<string> somehow? I know I could loop through
Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of
can you recommend some good ASP.NET tutorials or a good book? Should I jump
Can anyone tell me how I can display a status message like 12 seconds
Can you tell me what is the difference between abstraction and information hiding in
Can I get a 'when to use' for these and others? <% %> <%#
Can anyone recommend a good library for generating an audio file, such as mp3,
Can you suggest some good MVC framework for perl -- one I am aware

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.