tags:

views:

123

answers:

3

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

+2  A: 

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)))
Starkey
Ah, but this will close *all* buffers.
Sridhar Ratnakumar
+7  A: 

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

Sridhar Ratnakumar
This kills absolutely all buffers, not just the ones visiting files but also subprocesses, info, `*Messages*`, etc. If you replace `(buffer-list)` with `(remove-if-not 'buffer-file-name (buffer-list))`, only file-visiting buffers will be killed. Of course fancier filtering is possible.
Gilles
+2  A: 

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)
phils