I often leave Emacs (actually Aquamacs) open and wind up with a lot of buffers cluttering up my auto-complete. I would like to be able to close them all and start fresh. Right now I quit and reopen Emacs but that feels wrong.

Is there a way to close all buffers in Emacs?

This is Emacs 24.4 / Aquamacs 3.2.

  • (defun close-all-buffers () (interactive) (mapc 'kill-buffer (buffer-list))) -- stackoverflow.com/a/3417472/2112489 – lawlist Mar 30 '15 at 22:04
  • @lawlist Thanks! You should post that as an answer here so it's available under the right question title to be found by others. – Schwern Mar 31 '15 at 0:23
  • 1
    You may also check clean-buffer-list, which will close all buffers that have been unused for some time. – Juancho Mar 31 '15 at 14:07

The following function was written by username Starkey on stackoverflow in a related question: https://stackoverflow.com/a/3417472/2112489

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

EDIT:  As suggested by @Drew in the comment below, it is generally a good idea to keep internal buffers that have a leading space in their names. The doc-string provides an explanation of how this function works. The keyboard shortcut of the F5 key is just an example for the purposes of testing the function in conjunction with a universal argument.

(defun custom-kill-buffer-fn (&optional arg)
"When called with a prefix argument -- i.e., C-u -- kill all interesting
buffers -- i.e., all buffers without a leading space in the buffer-name.
When called without a prefix argument, kill just the current buffer
-- i.e., interesting or uninteresting."
(interactive "P")
    ((and (consp arg) (equal arg '(4)))
        (lambda (x)
          (let ((name (buffer-name x)))
            (unless (eq ?\s (aref name 0))
              (kill-buffer x))))
      (kill-buffer (current-buffer)))))

(global-set-key [f5] 'custom-kill-buffer-fn)
  • If you do that then you pretty much might as well be closing Emacs and restarting it. ;-) It is really unlikely that you want to kill all buffers. That includes "internal" buffers that Emacs pretty much expects to be there. You can do it, but I doubt that it is what you really want to do. I'd suggest maybe killing all buffers whose names do not start with a space character, for starters. – Drew Apr 1 '15 at 2:11
  • @Drew -- That's an excellent idea and it would give me an excuse to include an idea of my own, which is a kill-buffer function that accepts a universal-argument -- i.e., standard kill the current buffer, or kill them all that don't have a leading space. I'll add the new function to my to-do list. – lawlist Apr 1 '15 at 2:23
  • Great! I am doing C-u -- M-x custom-kill-buffer-fn, but it only kills the current buffer. Am I doing something wrong? – Emmanuel Goldstein Aug 11 '20 at 10:14

It seems to me, that your main problem are to many old buffers, that you probably have not visted for some time.

In emacs there is a command clean-buffer-list. By default it cleans buffers that have not been accessed for 3 days (or 1 hour, in the case of some special buffers). Of course, you can customize the time. Also have a look at midnight-mode, which does this automatically at some specified time.

If you wan't to kill all buffers, except the one you are currently using:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.