How can I close all buffers in Emacs?

1

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.

Schwern

Posted 2015-03-30T19:24:42.963

Reputation: 1 978

(defun close-all-buffers () (interactive) (mapc 'kill-buffer (buffer-list))) -- http://stackoverflow.com/a/3417472/2112489 – lawlist – 2015-03-30T22:04:41.807

@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 – 2015-03-31T00:23:19.650

1You may also check clean-buffer-list, which will close all buffers that have been unused for some time. – Juancho – 2015-03-31T14:07:40.870

Answers

3

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

(defun close-all-buffers ()
(interactive)
  (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")
  (cond
    ((and (consp arg) (equal arg '(4)))
      (mapc
        (lambda (x)
          (let ((name (buffer-name x)))
            (unless (eq ?\s (aref name 0))
              (kill-buffer x))))
        (buffer-list)))
    (t
      (kill-buffer (current-buffer)))))

(global-set-key [f5] 'custom-kill-buffer-fn)

lawlist

Posted 2015-03-30T19:24:42.963

Reputation: 1 287

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 – 2015-04-01T02:11:06.907

@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 – 2015-04-01T02:23:56.517

2

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."
  (interactive)
  (mapc 'kill-buffer (delq (current-buffer) (buffer-list))))

Christian Herenz

Posted 2015-03-30T19:24:42.963

Reputation: 366