Emacs: buffer-terminator.el – Safely Terminate Emacs Buffers Automatically

The buffer-terminator Emacs package automatically kills buffers to help maintain a clean and efficient workspace, while also improving Emacs’ performance by reducing the number of open buffers, thereby decreasing the number of active modes, timers, and other processes associated with those buffers.

Activating (buffer-terminator-mode) 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:

  • Special buffers (buffers whose names start with a space, start and end with *, or whose major mode is derived from special-mode).
  • Modified file-visiting buffers that have not been saved; the user must save them first.
  • Buffers currently displayed in any visible window.
  • Buffers associated with running processes.

(The default parameters above are fully customizable. Users can define specific rules for keeping or terminating certain buffers by specifying a set of rules using buffer-terminator-rules-alist. These rules can include buffer name patterns or regular expressions, major-modes, buffer properties, etc.)

Installation from MELPA

MELPA

To install buffer-terminator from MELPA:

  1. If you haven’t already done so, add MELPA repository to your Emacs configuration.
  2. Add the following code to the Emacs init file:
(use-package buffer-terminator
  :ensure t
  :custom
  (buffer-terminator-verbose nil)
  :config
  (buffer-terminator-mode 1))Code language: CSS (css)

Customizations

To enable verbose mode to log buffer cleanup events:

(setq buffer-terminator-verbose t)Code language: Lisp (lisp)

To set the inactivity timeout (in seconds) after which buffers are considered inactive (default is 30 minutes):

(setq buffer-terminator-inactivity-timeout (* 30 60)) ; 30 minutesCode language: Lisp (lisp)

To define how frequently the cleanup process should run (default is every 10 minutes):

(customize-set-variable 'buffer-terminator-interval (* 10 60)) ; 10 minutesCode language: Lisp (lisp)

(Using customize-set-variable allows buffer-terminator-interval to update the timer dynamically, without the need to restart buffer-terminator-mode.)

There are other customizable options available in the buffer-terminator.el README.md file.

Links