Close all buffers besides the current one in Emacs

EmacsElisp

Emacs Problem Overview


How do I close all but the current buffer in Emacs? Similar to "Close other tabs" feature in modern web browsers?

Emacs Solutions


Solution 1 - Emacs

For a more manual approach, you can list all buffers with C-x C-b, mark buffers in the list for deletion with d, and then use x to remove them.

I also recommend replacing list-buffers with the more advanced ibuffer: (global-set-key (kbd "C-x C-b") 'ibuffer). The above will work with ibuffer, but you could also do this:

m (mark the buffer you want to keep)
t (toggle marks)
D (kill all marked buffers)

I also use this snippet from the Emacs Wiki, which would further streamline this manual approach:

;; Ensure ibuffer opens with point at the current buffer's entry.
(defadvice ibuffer
  (around ibuffer-point-to-most-recent) ()
  "Open ibuffer with cursor pointed to most recent buffer name."
  (let ((recent-buffer-name (buffer-name)))
    ad-do-it
    (ibuffer-jump-to-buffer recent-buffer-name)))
(ad-activate 'ibuffer)

Solution 2 - Emacs

From EmacsWiki: Killing Buffers:

(defun kill-other-buffers ()
    "Kill all other buffers."
    (interactive)
    (mapc 'kill-buffer 
          (delq (current-buffer) 
                (remove-if-not 'buffer-file-name (buffer-list)))))

Edit: updated with feedback from Gilles

Solution 3 - Emacs

There isn't a way directly in emacs to do this.

You could write a function to do this. The following will close all the buffers:

(defun close-all-buffers ()
(interactive)
(mapc 'kill-buffer (buffer-list)))

Solution 4 - Emacs

There is a built in command m-x kill-some-buffers (I'm using 24.3.50) In my nextstep gui (not tried in a terminal but sure it's similar) you can then approve which buffers to kill.

Solution 5 - Emacs

 (defun only-current-buffer () 
   (interactive)
   (let ((tobe-killed (cdr (buffer-list (current-buffer)))))
     (while tobe-killed
       (kill-buffer (car tobe-killed))
       (setq tobe-killed (cdr tobe-killed)))))

It works as you expected.

And after reading @Starkey's answer, I think this will be better:

(defun only-current-buffer () 
  (interactive)                                                                   
    (mapc 'kill-buffer (cdr (buffer-list (current-buffer)))))

(buffer-list (current-buffer)) will return a list that contains all the existing buffers, with the current buffer at the head of the list.

This is my first answer on StackOverflow. Hope it helps :)

Solution 6 - Emacs

I found this solution to be the simplest one. This deletes every buffer except the current one. You have to add this code to your .emacs file

(defun kill-other-buffers ()
      "Kill all other buffers."
      (interactive)
      (mapc 'kill-buffer (delq (current-buffer) (buffer-list))))

Of course, then you use it with M-x kill-other-buffers RET or you paste the following code in the .emacs file too and then just press C-xC-b

(global-set-key (kbd "C-x C-b") 'kill-other-buffers)

Solution 7 - Emacs

You can like this one as well - kill all buffers except current one, *Messages* and *scratch* (which are handy to have, I call them "toolkit"), close redundant windows as well, living you which one window which current buffer.

(defun my/kill-all-buffers-except-toolbox ()
  "Kill all buffers except current one and toolkit (*Messages*, *scratch*). Close other windows."
  (interactive)
  (mapc 'kill-buffer (remove-if
                       (lambda (x)
                         (or
                           (eq x (current-buffer))
                           (member (buffer-name x) '("*Messages*" "*scratch*"))))
                       (buffer-list)))
  (delete-other-windows))

Solution 8 - Emacs

I've use crux-kill-other-buffers for some months.

But I want dired buffers get deleted too. @Euge's and @wenjun.yan's answers solve this. But it will delete special buffers (e.g *git-credential-cache--daemon*, *scratch*, helm operation, and etc). So I came up with this (current) solution.

(defun aza-kill-other-buffers ()
  "Kill all buffers but current buffer and special buffers"
  (interactive)
  (dolist (buffer (delq (current-buffer) (buffer-list)))
    (let ((name (buffer-name buffer)))
      (when (and name (not (string-equal name ""))
	         (/= (aref name 0) ?\s)
	         (string-match "^[^\*]" name))
    	(funcall 'kill-buffer buffer)))))

Inspired from kill-matching-buffers. You can add more condition on other buffer-name to exclude, if you want to.

Hope it helps :)

Solution 9 - Emacs

I've used one of the solutions in this list for years, but now I have a new one of my own.

(defun kill-all-file-buffers ()
  "Kills all buffers that are open to files. Does not kill
modified buffers or special buffers."
  (interactive)
  (mapc 'kill-buffer (cl-loop for buffer being the buffers
                              when (and (buffer-file-name buffer)
                                        (not (buffer-modified-p buffer)))
                              unless (eq buffer (current-buffer))
                              collect buffer)))

cl-loop has buffers built in as a collection that you can iterate over. It gives you a chance to parse out anything you don't want to close. Here, I've made sure that it doesn't close anything you've modified, and it uses buffer-file-name instead of just buffer-name so it doesn't kill special buffers. I also added an 'unless' to take out the current buffer (though you could obviously add it to the 'when', I just thought this was clearer).

But for an even more generic solution, we can define this as a macro, and pass in a function that will apply to all these buffers.

(defmacro operate-on-file-buffers (func)
  "Takes any function that takes a single buffer as an argument
and applies that to all open file buffers that haven't been
modified, and aren't the current one."
  `(mapc ,func (cl-loop for buffer being the buffers
                            when (and (buffer-file-name buffer)
                                      (not (buffer-modified-p buffer)))
                            unless (eq buffer (current-buffer))
                            collect buffer)))

Now if you want to kill all buffers that match this, you can call it like this

(operate-on-file-buffers 'kill-buffer)

Solution 10 - Emacs

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSridhar RatnakumarView Question on Stackoverflow
Solution 1 - EmacsphilsView Answer on Stackoverflow
Solution 2 - EmacsSridhar RatnakumarView Answer on Stackoverflow
Solution 3 - EmacsStarkeyView Answer on Stackoverflow
Solution 4 - EmacspjammerView Answer on Stackoverflow
Solution 5 - Emacswenjun.yanView Answer on Stackoverflow
Solution 6 - EmacsEugeView Answer on Stackoverflow
Solution 7 - EmacsNavidotView Answer on Stackoverflow
Solution 8 - EmacsazzamsaView Answer on Stackoverflow
Solution 9 - EmacsRealityMonsterView Answer on Stackoverflow
Solution 10 - EmacssiviView Answer on Stackoverflow