The buffer-terminator Emacs package automatically and safely kills buffers, ensuring a clean and efficient workspace while enhancing the performance of Emacs by reducing open buffers, which minimizes active modes, timers, processes...
Beyond performance, buffer-terminator provides other benefits. For instance, if you occasionally need to close annoying or unused buffers, buffer-terminator can handle this automatically, eliminating the need for manual intervention. (The default configuration is suitable for most users. However, the buffer-terminator package is highly customizable. You can define specific rules for retaining or terminating buffers by modifying the buffer-terminator-rules-alist
with your preferred set of rules.)
Activating (buffer-terminator-mode)
safely terminates all buffers that have been inactive for longer than the duration specified by buffer-terminator-inactivity-timeout
(default: 30 minutes). It checks every buffer-terminator-interval
(default: 10 minutes) to determine if a buffer should be terminated.
The following buffers are not terminated by default:
*
, or whose major mode is derived from special-mode
, or they serve as the Minibuffer).org-src
source edit buffers, which cause their originating buffers to be considered visible, or markdown-mode
edit-indirect buffers that reference the original Markdown file.)To install buffer-terminator from MELPA:
(use-package buffer-terminator :ensure t :custom (buffer-terminator-verbose nil) ;; Set the inactivity timeout (in seconds) after which buffers are considered ;; inactive (default is 30 minutes): (buffer-terminator-inactivity-timeout (* 30 60)) ; 30 minutes ;; Define how frequently the cleanup process should run (default is every 10 ;; minutes): (buffer-terminator-interval (* 10 60)) ; 10 minutes :config (buffer-terminator-mode 1))
Enable verbose mode to log buffer cleanup events:
(setq buffer-terminator-verbose t)
Set the inactivity timeout (in seconds) after which buffers are considered inactive (default is 30 minutes):
(setq buffer-terminator-inactivity-timeout (* 30 60)) ; 30 minutes
Define how frequently the cleanup process should run (default is every 10 minutes):
(customize-set-variable 'buffer-terminator-interval (* 10 60)) ; 10 minutes
(Using customize-set-variable
allows buffer-terminator-interval
to update the timer dynamically, without the need to restart buffer-terminator-mode
.)
By default, buffer-terminator automatically determines which buffers are safe to terminate.
However, if you need to define specific rules for keeping or terminating certain buffers, you can configure them using buffer-terminator-rules-alist
.
The buffer-terminator-rules-alist
variable holds instructions for keeping or terminating buffers based on their names or regular expressions. Each rule is a cons cell where the key is a symbol indicating the rule type, and the value is either string or a list of strings.
Here is an example:
(setq buffer-terminator-rules-alist ;; kill-buffer-name: Always kill buffers whose names match important-buffer-name1 and important-buffer-name2 '((kill-buffer-name . ("temporary-buffer-name1" "temporary-buffer-name2")) ;; keep-buffer-name: Always keep buffers whose names match important-buffer-name1 and important-buffer-name2 (keep-buffer-name . ("important-buffer-name1" "important-buffer-name2")) ;; kill-buffer-name: Always kill buffers whose names match temporary-buffer-name3 (kill-buffer-name . "temporary-buffer-name3") ;; keep-buffer-name-regexp: Always keep buffers matching a regular expression (keep-buffer-name-regexp . ("\\` \\*Minibuf-[0-9]+\\*\\'")) ;; kill-buffer-name-regexp: Always kill buffers matching a regular expression (kill-buffer-name-regexp . "compile-angel") ;; Retain special buffers (DO NOT REMOVE). ;; ;; (If you choose to kill special buffers by removing the following, ;; ensure that the special buffers you want to keep are added ;; keep-buffer-name or keep-buffer-name-regexp rules above.) ;; ;; DO NOT REMOVE special buffers unless you know of what you are doing. (keep-buffer-property . special) ;; Retain process buffers. ;; ;; (Process buffers are buffers where an active process is running. ;; Removing the following will result in the termination of such ;; buffers, potentially disrupting active processes like vterm.) (keep-buffer-property . process) ;; Retain visible buffers (DO NOT REMOVE). ;; ;; Visible buffer are those currently displayed in any window. ;; It is generally discouraged to set this to nil, as doing so may result ;; in the termination of visible buffers, except for the currently active ;; buffer in the selected window. ;; ;; DO NOT REMOVE visible buffers unless necessary. (keep-buffer-property . visible) ;; Kill inactive buffers. ;; (This can be customized with `buffer-terminator-inactivity-timeout' ;; and `buffer-terminator-interval'.) (kill-buffer-property . inactive) ;; Call a function that decides the fate of a buffer. It returns: ;; :kill Indicates that the buffer should be killed. ;; :keep Indicates that the buffer should be kept. ;; nil Leave decision to the next rule specified ;; in `buffer-terminator-rules-alist`. ;; (call-function . function-name) ;; Keep the remaining buffers that were not retained by previous rules (return . :keep)))
Here is another example by gavv, one of the first buffer-terminator users.
Frequently asked questions What problem is buffer-terminator aiming to solve?Because each Emacs user's configuration is unique, the performance benefits of using the buffer-terminator Emacs package depend on the number of enabled modes and active timers in that specific setup.
Leaving buffers open keeps their associated timers active, and the number of timers grows with the number of Emacs packages in use. Timers are responsible for scheduling functions to run at specific intervals or after a delay, often managing background tasks like updating buffers, fetching data, or performing periodic checks. Since each active timer triggers a function, an excessive number of timers can increase CPU usage, potentially leading to performance degradation in Emacs' single-threaded environment.
Additionally, using buffer-terminator to reduce the buffer list can improve the performance of packages that iterate over the buffer-list
function to operate on buffers. Since these packages iterate over open buffers, a shorter buffer list allows for faster execution of their operations. (For example, the built-in desktop.el package or the easysession package, which save and restore open buffers/frames, can be affected by buffer list length. If buffer-list
is too long, Emacs startup may slow down, as it needs to restore a larger set of buffers.)
There is little benefit in leaving unused buffers open on the off-chance they might be needed later. If needed again, these buffers can be quickly reopened using recentf, project.el, dired, or similar tools.
How is this different from the builtin midnight-mode?Midnight-mode does not address the problem that buffer-terminator solves, which is the safe and frequent termination of inactive buffers.
Midnight mode and clean-buffer-list
are for killing buffers once a day. The Midnight option clean-buffer-list-delay-general
specifies the number of days before a buffer becomes eligible for auto-killing, rather than using seconds or minutes as a timeout.
In contrast, buffer-terminator
allows specifying the timeout interval in seconds (Default: 30 minutes), enabling more frequent termination of inactive buffers.
The buffer-terminator
package offers additional features that are not supported by midnight and clean-buffer-list
, including:
buffer-terminator-rules-alist
, enabling them to determine which buffers should be killed based on factors such as inactivity, visibility, buffer name, whether it's a file or process buffer, and other conditions.buffer-display-time
, which is not always updated reliably. For instance, buffer-display-time
may not reflect activity when switching to a window or tab displaying a specific buffer.The buffer-terminator Emacs package has been written by James Cherti and is distributed under terms of the GNU General Public License version 3, or, at your choice, any later version.
Copyright (C) 2024-2025 James Cherti
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program.
Other Emacs packages by the same author:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4