help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Remove all directory listings buffers


From: Drew Adams
Subject: RE: Remove all directory listings buffers
Date: Sat, 28 Nov 2009 11:46:55 -0800

> But I also always end up having too many buffers, I need a better
> strategy for killing them when I don't need them anymore..

I think some people are in the habit of using `C-x 0' to remove the current
window, even when they no longer need the window's buffer.

Why? Because they don't want some other buffer taking its place (if they were to
use `C-x k' instead). IOW, what they really want is both `C-x k' and `C-x 0',
but that is too much to type each time they just want to get something out of
the way.

I use this as a replacement for `kill-buffer' interactively (only):

(defun kill-buffer-and-its-windows (buffer)
  "Kill BUFFER and delete its windows.
Default is `current-buffer'.
BUFFER can be either a buffer or its name (a string)."
  (interactive
   (list (read-buffer "Kill buffer: "
                      (current-buffer) 'existing)))
  (setq buffer (get-buffer buffer))
  (cond ((buffer-live-p buffer)
         (let ((wins (get-buffer-window-list buffer nil t)))
           (when (kill-buffer buffer)
             (dolist (win wins)
               (when (window-live-p win)
                (delete-window win))))))
        ((interactive-p)
         (error
          "Cannot kill buffer.  Not a live buffer: `%s'"
          buffer))))

;; This points all keys and menus that would normally use
;; `kill-buffer' to `kill-buffer-and-its-windows' instead.
(substitute-key-definition
  'kill-buffer 'kill-buffer-and-its-windows global-map)

And I'm in the habit of using `C-x k' when I'm through with a buffer and its
window. I use `C-x 0' only when I explicitly want to keep the buffer around.

FWIW, this code is in library misc-cmds.el (and setup-keys.el for the key
mapping).
http://www.emacswiki.org/emacs/misc-cmds.el
http://www.emacswiki.org/emacs/setup-keys.el





reply via email to

[Prev in Thread] Current Thread [Next in Thread]