vimtricks.wiki Concise Vim tricks, one at a time.

How do I close all tabs and buffers except the current one?

Answer

:tabonly | %bdelete | edit #

Explanation

When your Vim session becomes cluttered with many tabs and buffers, you can clean up by closing all tabs except the current one with :tabonly, then deleting all buffers with :%bdelete, and reopening the current file with :edit #.

How it works

  • :tabonly — close all tab pages except the current one
  • :%bdelete — delete all buffers (but keeps the window)
  • :edit # — reopen the alternate file (your current file before bdelete)
  • Combined: cleans up everything and leaves you with just one file

Example

" Close everything except current file
:tabonly | %bdelete | edit #
Before: 5 tabs, 15 buffers open
After:  1 tab, 1 buffer (your current file)

Tips

  • Use :%bdelete | edit # alone if you don't use tabs
  • :bufdo bdelete is an alternative that processes buffers one by one
  • Add ! to force close modified buffers: :%bdelete!
  • For buffers only: :1,1000bdelete deletes buffers 1-1000 (skips current if not in range)

Next

How do I return to normal mode from absolutely any mode in Vim?