简体   繁体   中英

Close all temporary buffers automatically in emacs

How can we close temporary buffers which are enclosed with * automatically. For eg messages , completions buffer needs to be closed. Killing all these buffers manually after use is painful.

Is there a way to close temporary buffers created by emacs (not by us)?

Do you really need to close those buffers? If you use a proper buffer switching method like iswitchb then you don't have to care about temporary or other buffers, because you can go directly to any buffer you want.

I'd second the suggestion you use ido or iswitchb to avoid being bothered by temporary buffers. The presence of those buffers is a natural consequence of using emacs, so don't try to swim upstream!

On the other hand, if you're irritated by the growing list of open buffers, you can use midnight.el to automatically close inactive buffers after a period of time, or you can use ibuffer to easily select and close unwanted buffers en masse.

Personally, I leave buffers open for a long time, I tidy them up occasionally using ibuffer , and I rely on ido to switch buffers quickly. In Emacs 24, you can set ido-use-virtual-buffers to t , and then ido will let you switch to closed files, reopening them as necessary.

As "user" said, it would be better to use a smart buffer-switching package such as iswitchb and ido . iswitchb 's iswitchb-buffer-ignore and ido 's ido-ignore-buffers variables allow us to specify what buffers to ignore using regular expressions.

However, if you really want to kill those buffers, a program like this will be helpful to you:

(require 'cl)

(defvar kill-star-buffers-except
  '("\\`\\*scratch\\*\\'"
    "\\`\\*Messages\\*\\'"
    "\\` \\*Minibuf-[[:digit:]]+\\*\\'"
    "\\` \\*Echo Area [[:digit:]]+\\*\\'")
  "Exception list for `kill-star-buffers'")

(defun kill-star-buffers ()
  "Kill all star buffers except those in `kill-star-buffers-except'"
  (interactive)
  (mapc (lambda (buf)
          (let ((buf-name (buffer-name buf)))
            (when (and
                   ;; if a buffer's name is enclosed by * with optional leading
                   ;; space characters
                   (string-match-p "\\` *\\*.*\\*\\'" buf-name)
                   ;; and the buffer is not associated with a process
                   ;; (suggested by "sanityinc")
                   (null (get-buffer-process buf))
                   ;; and the buffer's name is not in `kill-star-buffers-except'
                   (notany (lambda (except) (string-match-p except buf-name))
                           kill-star-buffers-except))
              (kill-buffer buf))))
        (buffer-list)))

To avoid having those buffers in your way, you could define key-bindings to cycle through «user buffers» and «useless buffers» :

http://ergoemacs.org/emacs/effective_emacs.html , section «Switching Next/Previous User Buffers»

but some useful buffers start with a *, like shells, compilation buffer, ielm, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM