Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close all buffers besides the current one in Emacs

Tags:

emacs

elisp

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

like image 659
Sridhar Ratnakumar Avatar asked Aug 05 '10 17:08

Sridhar Ratnakumar


People also ask

How do I close all buffers in Emacs?

M-x kill-matching-buffers. Offer to kill all buffers matching a regular expression. C-x k ( kill-buffer ) kills one buffer, whose name you specify in the minibuffer. The default, used if you type just RET in the minibuffer, is to kill the current buffer.

How do I close all buffers in Vim?

Just put it to your . vim/plugin directory and then use :BufOnly command to close all buffers but the active one.

How do I close a window in Emacs?

To close the current window, type C-x 0 (zero this time, not O). To close all windows except the current one, type C-x 1.

What are buffers in Emacs?

Buffers in Emacs editing are objects that have distinct names and hold text that can be edited. Buffers appear to Lisp programs as a special data type. You can think of the contents of a buffer as a string that you can extend; insertions and deletions may occur in any part of the buffer.


2 Answers

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) 
like image 161
phils Avatar answered Oct 10 '22 16:10

phils


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

like image 30
Sridhar Ratnakumar Avatar answered Oct 10 '22 16:10

Sridhar Ratnakumar