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)