How to kill (C-x k) ediff's (A,B,C) buffers automatically on each ediff-quit
?
-
Did you really mean close the buffers, as opposed to closing the *windows*, i.e. usually going back to a single window showing a single file (but leaving the files open)? – Gilles 'SO- stop being evil' Oct 12 '15 at 21:58
-
Yes, I mean kill buffers (C-x k) – denys Oct 13 '15 at 06:50
-
Why do you wish to do this? Often when I do an ediff I need to continue work on one of the buffers afterwards. – Andrew Swann Oct 16 '15 at 07:09
-
In my case I'm using ztree-diff to view the diffs and I'm not interested in the files after the diff most of the time. – Alwyn Schoeman Feb 26 '20 at 23:05
5 Answers
(defun my-kill-ediff-buffers ()
(kill-buffer ediff-buffer-A)
(kill-buffer ediff-buffer-B)
(kill-buffer ediff-buffer-C))
(add-hook 'ediff-quit-hook 'my-kill-ediff-buffers)
Note that this will always kill ediff-buffers, even if they weren't opened by ediff e.g. you called ediff-buffers
with some buffers you had open to start with.
If you want to you could also kill *ediff-errors*
, *ediff-diff*
, *Ediff-Registry*
and *ediff-fine-diff*
as well, but those generally don't bother me.

- 719
- 5
- 11
I'm adding an answer because this is the first link that popped up for me on this topic, and this is indeed way simpler.
You can pass a prefix to "q" and it will prompt to kill the ediff buffers.
C-u q
If you customize ediff-keep-variants
it will reverse the behaviour: prompt to kill the buffers on exit and if use the prefix will leave them open.
(got this from the ediff manual)

- 21
- 5
I'm using this code (GNU Emacs 25.3.1 x86_64-pc-linux-gnu), following https://emacs.stackexchange.com/a/17089/18662
As stated in the GNU Emacs Manual regarding ediff-quit-hook (https://www.gnu.org/software/emacs/manual/html_node/ediff/Hooks.html):
Keep in mind that hooks executing before ediff-cleanup-mess start in ediff-control-buffer; they should also leave ediff-control-buffer as the current buffer when they finish. Hooks that are executed after ediff-cleanup-mess should expect the current buffer be either buffer A or buffer B.
So, I kill also the *Ediff Control Panel* buffer. Other ediff buffers may be added too.
(setq ediff-window-setup-function 'ediff-setup-windows-plain)
(setq ediff-split-window-function 'split-window-horizontally)
(defvar q-ediff-last-windows nil)
(defun q-store-pre-ediff-winconfig ()
(setq q-ediff-last-windows (current-window-configuration)))
(defun q-restore-pre-ediff-winconfig ()
(progn
(set-window-configuration q-ediff-last-windows)
(ediff-kill-buffer-carefully "*Ediff Control Panel*")))
(add-hook 'ediff-before-setup-hook #'q-store-pre-ediff-winconfig)
(add-hook 'ediff-quit-hook #'q-restore-pre-ediff-winconfig)

- 131
- 2
-
+10 for `(setq ediff-split-window-function 'split-window-horizontally)` alone! Was searching for a possibility to always split horizontally in `ztree-diff`. Thank you! – Ugur Jan 16 '21 at 16:39
You can try this hook:
(add-hook 'ediff-load-hook
(lambda ()
(add-hook 'ediff-before-setup-hook
(lambda ()
(setq ediff-before-file (buffer-file-name))
(setq ediff-saved-window-configuration (current-window-configuration))))
(let ((restore-window-configuration
(lambda ()
(set-window-configuration ediff-saved-window-configuration))))
(add-hook 'ediff-quit-hook restore-window-configuration 'append)
(add-hook 'ediff-suspend-hook restore-window-configuration 'append))))

- 25,203
- 3
- 74
- 179

- 3,169
- 16
- 31
-
2It is not recommended to bind `lambda` functions to hooks (and advices). It makes reviewing the hook variables and removing functions from hooks painful. – Kaushal Modi Oct 12 '15 at 21:00
-
2This effectively closes the windows, but the question is asking to kill the **buffers**. – Gilles 'SO- stop being evil' Oct 13 '15 at 12:15
This is what I am using to close the windows:
(add-hook 'ediff-after-quit-hook-internal 'winner-undo)

- 21,702
- 4
- 53
- 89

- 3,401
- 13
- 40
-
1This effectively closes the windows, but the question is asking to kill the **buffers**. – Gilles 'SO- stop being evil' Oct 13 '15 at 12:15