‘kill-buffer-and-its-windows’, available in library Lisp:misc-cmds.el, kills the buffer and deletes all of its windows. The default buffer to kill is
‘current-buffer’. The argument to the command may be either a buffer or its name (a string).
This command is especially useful as a replacement for command
‘kill-buffer’ when using Emacs Frames – see OneOnOneEmacs. Used with other frame-oriented functions, it also deletes the frame if the buffer is alone in the frame.
‘kill-buffer’displays a different buffer in the window, in place of one you’ve killed. With frames this is more annoying than useful.
‘kill-buffer-and-window’just complains if you try to delete the sole window in a frame.
Note: You can use
‘substitute-key-definition’ to replace all key bindings for
‘kill-buffer’ with bindings to
(substitute-key-definition 'kill-buffer 'kill-buffer-and-its-windows global-map)
Here is a simple function that kills all buffers, except the current one.
(defun kill-other-buffers () "Kill all other buffers." (interactive) (mapc 'kill-buffer (delq (current-buffer) (buffer-list))))
Here is a simple, one-off command to kill all dired buffers:
(defun kill-all-dired-buffers() "Kill all dired buffers." (interactive) (save-excursion (let((count 0)) (dolist(buffer (buffer-list)) (set-buffer buffer) (when (equal major-mode 'dired-mode) (setq count (1+ count)) (kill-buffer buffer))) (message "Killed %i dired buffer(s)." count ))))
To use this for BSBufferSelection, consider the following keybinding:
(define-key bs-mode-map (kbd "D D") 'kill-all-dired-buffers)
See also: KillKey
Many times a temporary window comes up for help or compilation results, auto complete, buffer list, etc. To close that one has to switch to the other window and kill the buffer. After killing the buffer, another buffer gets shown in that window and so we need to close the window to restore the original buffer with full size.
These two functions solve this problem. You can bind your un-used keys to these functions for quick access.
(defun close-and-kill-next-pane () "Close the other pane and kill the buffer in it also." (interactive) (other-window 1) (close-current-buffer) (delete-window) )
(defun close-and-kill-this-pane () "Close this pane and kill the buffer in it also." (interactive) (close-current-buffer) (delete-window) )
Killing all Dired buffers is easy with IbufferMode: simply type C-x C-b * / D yes RET.
Here’s one way to kill Dired buffers using Icicles.
C-!’ to kill them all.
Here’s how to define a command that lets you do that. You need:
Kill one or more Dired buffers
‘completing-read’to read a Dired buffer name:
Kill Dired buffer:
‘buffer-name’) over the buffer list
(icicle-define-command kill-dired-buffer ; Command name "Kill one or more Dired buffers" ; Doc string kill-buffer ; Function to perform the action "Kill Dired buffer: " ; Prompt (mapcar (lambda (buf) (list (buffer-name buf))) (buffer-list)) ; Completions (lambda (bufcons) (save-excursion ; Predicate: Dired mode? (set-buffer (car bufcons)) (equal major-mode 'dired-mode))) t) ; Existing buffers only
How does the Icicles version compare with the other methods described above?
C-!’ is an extra step that just gets in the way.
C-!’). There is no special UI for users to learn. There are a few things to learn (e.g. `
C-!’), but they are not particular to one kind of object (buffers) or action (deleting).
There is a place for specialized user interfaces, and a place for a more common, uniform UI. If IbufferMode (just as an example) is especially well designed to manage buffers, then why not learn its interface and use it? On the other hand, if the same interface can be used for operations of all kinds on all kinds of objects, and if those operations are simple enough to perform, then less learning is needed.
For instance, to give you an idea of how “simple” it is to learn the `* / D’ incantation shown above (in UsingIbufferMode), here are a few of the bindings for IbufferMode (about 1/4 of those listed in the
‘ibuffer-mode’ doc string). These are just the keys for marking buffers. The second part of the incantation,
‘D’, is listed in a separate section, on keys for operating on marked buffers. The KeySequences follow a logical pattern, but there is still a lot to learn.
'm' - Mark the buffer at point. 't' - Unmark all currently marked buffers, and mark all unmarked buffers. 'u' - Unmark the buffer at point. 'DEL' - Unmark the buffer at point, and move to the previous line. '* *' - Unmark all marked buffers. '* M' - Mark buffers by major mode. '* u' - Mark all "unsaved" buffers. This means that the buffer is modified, and has an associated file. '* m' - Mark all modified buffers, regardless of whether or not they have an associated file. '* s' - Mark all buffers whose name begins and ends with '*'. '* e' - Mark all buffers which have an associated file, but that file doesn't currently exist. '* r' - Mark all read-only buffers. '* /' - Mark buffers in `dired' mode. '* h' - Mark buffers in `help-mode', `apropos-mode', etc. '.' - Mark buffers older than `ibuffer-old-time'. 'd' - Mark the buffer at point for deletion. '% n' - Mark buffers by their name, using a regexp. '% m' - Mark buffers by their major mode, using a regexp. '% f' - Mark buffers by their filename, using a regexp.
The aim in pointing this out is not to trash IbufferMode – not at all. It’s just to ponder the notion of simplicity of use.
At some point, of course, there is a trade-off:
The question for the second approach is, “How flexible and general is the mold?” A hammer is a good, general tool – the key is to recognize when a hammer is no longer appropriate for the task at hand. – DrewAdams
Here’s a function that you can use instead of kill-this-buffer, but which prevents some buffers to be killed by mistake :
;; you can modify that list, to fit your needs (setq not-to-kill-buffer-list '("*scratch*" "#emacs" "*Messages*" "irc.freenode.net:6667")) (defun kill-buffer-but-not-some () (interactive) (if (member (buffer-name (current-buffer)) not-to-kill-buffer-list) (bury-buffer) (kill-buffer (current-buffer))))
When you enter IswitchBuffers, press C-k to kill selected buffer. I always use this method to kill certain buffers. – Anonymous
To protect buffers from being killed, see ProtectingBuffers.
There is a MidnightMode to clear unused buffers at midnight. I find that I need a more aggressive clear up. Here is what I use to auto kill buffers not active for more than 15 minutes every 2 hours.
;; midnight mode
;;kill buffers if they were last disabled more than this seconds ago (setq clean-buffer-list-delay-special 900)
(defvar clean-buffer-list-timer nil "Stores clean-buffer-list timer if there is one. You can disable clean-buffer-list by (cancel-timer clean-buffer-list-timer).")
;; run clean-buffer-list every 2 hours (setq clean-buffer-list-timer (run-at-time t 7200 'clean-buffer-list))
;; kill everything, clean-buffer-list is very intelligent at not killing ;; unsaved buffer. (setq clean-buffer-list-kill-regexps '("^.*$"))
;; keep these buffer untouched ;; prevent append multiple times (defvar clean-buffer-list-kill-never-buffer-names-init clean-buffer-list-kill-never-buffer-names "Init value for clean-buffer-list-kill-never-buffer-names") (setq clean-buffer-list-kill-never-buffer-names (append '("*Messages*" "*cmd*" "*scratch*" "*w3m*" "*w3m-cache*" "*Inferior Octave*") clean-buffer-list-kill-never-buffer-names-init))
;; prevent append multiple times (defvar clean-buffer-list-kill-never-regexps-init clean-buffer-list-kill-never-regexps "Init value for clean-buffer-list-kill-never-regexps") ;; append to *-init instead of itself (setq clean-buffer-list-kill-never-regexps (append '("^\\*EMMS Playlist\\*.*$") clean-buffer-list-kill-never-regexps-init))
Anything is a candidate selection framework.
Start with M-x anything-kill-buffers, narrow the list by typing some patterns(multiple patterns are space-delimited string), select with up/down/pgup/pgdown/C-p/C-n/C-v/M-v, choose with enter, With TAB actions can be selected if the selected buffer has more than one possible action. With C-z kills buffer without quitting anything session.
Say, you want to kill buffers for asynchronous processes by default 10 minutes after the corresponding process exits. For that purpose you can use dying-mode from dying.el and the code below in your .emacs file.
You can set the default lifetime of the buffer after the process exits with the variable dying-default-lifetime. If you consider the process output to be important to you for a longer time than dying-default-lifetime you can switch off dying-mode within the lifetime of the process buffer or prolong the lifetime of the buffer.
(require 'dying) (unless (fboundp 'start-process-orig) (fset 'start-process-orig (symbol-function 'start-process))) (fset 'start-process (append (list 'lambda '(name buffer program &rest program-args) (concat (let ((s (documentation 'start-process))) (substring s 0 (string-match "\n\n.*\\'" s))) "\nAfter finishing the process the process buffer goes into dying-mode.")) '((let ((ret (apply 'start-process-orig name buffer program program-args))) (if ret (set-process-sentinel ret '(lambda (proc event) (when (and (eq (process-status proc) 'exit) (process-buffer proc)) (with-current-buffer (process-buffer proc) (dying-mode t)))))) ret)))) (defadvice shell-command-sentinel (after shell-command-sentinel-dying-mode (proc event) activate) "The original shell-command-sentinel prevents insertion of a termination message into the process buffer itself. The advice starts dying-mode after the process exits." (when (and (eq (process-status proc) 'exit) (process-buffer proc)) (with-current-buffer (process-buffer proc) (dying-mode t))))