https://github.com/jamescherti/buffer-guardian.el
buffer-guardian.el - Automatically save Emacs buffers without manual intervention
https://github.com/jamescherti/buffer-guardian.el
backup buffer emacs emacs-package emacs-packages save save-buffer
Last synced: 1 day ago
JSON representation
buffer-guardian.el - Automatically save Emacs buffers without manual intervention
- Host: GitHub
- URL: https://github.com/jamescherti/buffer-guardian.el
- Owner: jamescherti
- License: gpl-3.0
- Created: 2026-03-14T15:34:55.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2026-05-21T00:25:05.000Z (9 days ago)
- Last Synced: 2026-05-21T02:29:03.177Z (9 days ago)
- Topics: backup, buffer, emacs, emacs-package, emacs-packages, save, save-buffer
- Language: Emacs Lisp
- Homepage: https://github.com/jamescherti/buffer-guardian.el
- Size: 178 KB
- Stars: 20
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# buffer-guardian.el - Automatically Save Emacs Buffers Without Manual Intervention (When Buffers Lose Focus, Regularly, or After Emacs is Idle)


[](https://melpa.org/#/buffer-guardian)
[](https://stable.melpa.org/#/buffer-guardian)

The **buffer-guardian** Emacs package provides `buffer-guardian-mode`, a global mode that automatically saves buffers without requiring manual intervention.
**By default, `buffer-guardian-mode` saves file-visiting buffers when:**
- Switching to another buffer.
- Switching to another window or frame.
- The window configuration changes (e.g., window splits).
- The minibuffer is opened.
- Emacs loses focus.
(Skip to: [Installation](#Installation))
In addition to regular file-visiting buffers, `buffer-guardian-mode` also handles specialized editing buffers used for inline code blocks, such as `org-src` (for Org mode) and `edit-indirect` (commonly used for Markdown source code blocks). These temporary buffers are linked to an underlying parent buffer. Automatically saving them ensures that modifications made within these isolated code environments are correctly propagated back to the original Org or Markdown file.
The `buffer-guardian-mode` automatically detects active completion interfaces, such as `corfu` or `completion-in-region-mode`, and suppresses background save operations (controlled by the variable `buffer-guardian-inhibit-save-on-completion`, which defaults to t). This prevents premature auto-save triggers from invoking external hooks like automated code formatters, linters, or language server processes, which eliminates potential UI latency or race conditions.
If this package enhances your workflow, please show your support by **⭐ starring buffer-guardian on GitHub** to help more users discover its benefits.
Other features that are **disabled** by default:
- Save the buffer even if a window change results in the same buffer being selected. (Variable: `buffer-guardian-save-on-same-buffer-window-change`)
- Save all file-visiting buffers periodically at a specific interval. (Variable: `buffer-guardian-save-all-buffers-interval`)
- Save all file-visiting buffers after a period of user inactivity. (Variable: `buffer-guardian-save-all-buffers-idle`)
- Pre-save buffers silently before global save and exit commands. (Variable: `buffer-guardian-override-save-some-buffers`)
- Prevent auto-saving remote files. (Variable: `buffer-guardian-inhibit-saving-remote-files`)
- Prevent saving files that do not exist on disk. (Variable: `buffer-guardian-inhibit-saving-nonexistent-files`)
- Set a maximum buffer size limit for auto-saving. (Variable: `buffer-guardian-max-buffer-size`)
- Ignore buffers whose names match specific regular expressions. (Variable: `buffer-guardian-exclude-regexps`)
- Use custom predicate functions to determine if a buffer should be saved. (Variable: `buffer-guardian-predicate-functions`)
(Buffer Guardian runs in the background without interrupting the workflow. For example, the package safely aborts the auto-save process if the file is read-only, if the file's parent directory does not exist, or if the file was modified externally. It also implements a dual-edge debouncing mechanism that fires an immediate synchronous save on the first trigger and a final trailing save when user activity stops, completely preventing editor freezes and saving race conditions. Additionally, it gracefully catches and logs errors if a third-party hook attempts to request user input, ensuring that the editor never freezes during an automatic background save.)
## Installation
To install **buffer-guardian** from MELPA:
1. If you haven't already done so, [add MELPA repository to your Emacs configuration](https://melpa.org/#/getting-started).
2. Add the following code to your Emacs init file to install **buffer-guardian** from MELPA:
```emacs-lisp
(use-package buffer-guardian
:custom
;; When non-nil, include remote files in the auto-save process
(buffer-guardian-inhibit-saving-remote-files t)
;; When non-nil, buffers visiting nonexistent files are not saved
(buffer-guardian-inhibit-saving-nonexistent-files nil)
;; Save the buffer even if the window change results in the same buffer
(buffer-guardian-save-on-same-buffer-window-change t)
;; Non-nil to enable verbose mode to log when a buffer is automatically saved
(buffer-guardian-verbose nil)
;; Pre-save all package-managed buffers before native save commands run
;; Advise `save-some-buffers' to use `buffer-guardian' logic.
;; When non-nil and `buffer-guardian-mode' is active, this intercepts
;; `save-some-buffers' to silently pre-save package-managed buffers before
;; allowing the native command to run normally.
(buffer-guardian-override-save-some-buffers nil)
;; Save all buffers after N seconds of user idle time. (Disabled by default)
;; (buffer-guardian-save-all-buffers-idle 30)
;; Save all buffers every N seconds. (Disabled by default)
;; (setq buffer-guardian-save-all-buffers-interval (* 60 30))
:hook
(after-init . buffer-guardian-mode))
```
## Configuration
You can customize **buffer-guardian** to fit your workflow. Below are the main customization variables:
### Triggers
* `buffer-guardian-save-on-focus-loss` (Default: `t`): Save when the Emacs frame loses focus.
* `buffer-guardian-save-on-minibuffer-setup` (Default: `t`): Save when the minibuffer opens.
* `buffer-guardian-save-on-buffer-switch` (Default: `t`): Save when `window-buffer-change-functions` runs.
* `buffer-guardian-save-on-window-selection-change` (Default: `t`): Save when `window-selection-change-functions` runs.
* `buffer-guardian-save-on-window-configuration-change` (Default: `t`): Save when `window-configuration-change-hook` runs.
* `buffer-guardian-save-on-same-buffer-window-change` (Default: `nil`): Save the buffer even if the window change results in the same buffer.
### Timers
* `buffer-guardian-save-all-buffers-interval` (Default: `nil`): Save all buffers periodically every N seconds.
* `buffer-guardian-save-all-buffers-idle` (Default: `nil`): Save all buffers after N seconds of user idle time.
### Exclusions and Filters
* `buffer-guardian-inhibit-saving-remote-files` (Default: `t`): Prevent auto-saving remote files.
* `buffer-guardian-inhibit-saving-nonexistent-files` (Default: `t`): Prevent saving files that do not exist on disk.
* `buffer-guardian-exclude-regexps` (Default: `nil`): A list of regular expressions for file names to ignore.
* `buffer-guardian-max-buffer-size` (Default: `nil`): Maximum buffer size (in characters) to save. Set to 0 or nil to disable.
* `buffer-guardian-predicate-functions` (Default: `nil`): List of predicate functions to determine if a buffer should be saved.
### Specialized Buffers (Inline Code Blocks)
* `buffer-guardian-handle-org-src` (Default: `t`): Enable automatic saving for `org-src` buffers.
* `buffer-guardian-handle-edit-indirect` (Default: `t`): Enable automatic saving for `edit-indirect` buffers.
### Advanced
- `buffer-guardian-override-save-some-buffers` (Default: `nil`): Advises `save-some-buffers` to execute a silent save of all buffers before letting the native `save-some-buffers` logic run. When this option is enabled, triggering commands like `save-buffers-kill-emacs` cleanly writes your modified project files to disk first, drastically minimizing or entirely bypassing subsequent interactive prompts for those buffers.
* `buffer-guardian-save-all-buffers-trigger-hooks`: A list of hooks that trigger saving all modified buffers. Defaults to nil.
* `buffer-guardian-save-trigger-functions`: A list of functions to advise. A `:before` advice will save the current buffer before these functions execute.
* `buffer-guardian-verbose` (Default: `nil`): Enable logging messages when a buffer is saved.
### Manual Saving Without Interruption
While `buffer-guardian-mode` handles background saves automatically, you can explicitly force a safe, background-style save at any time using the `buffer-guardian-save-buffer` command.
Replacing the default Emacs `save-buffer` with this command provides several direct improvements to your daily editing workflow:
* It actively guards against overwriting external changes. If a file is modified outside of Emacs, the command gracefully intercepts the save operation and presents clear, interactive choices to discard your edits, force an overwrite, or cancel safely.
* Running it inside temporary or isolated buffers like `org-src` (for Org mode source blocks) or `edit-indirect` (for Markdown code blocks) correctly commits changes straight back to the underlying parent file.
To replace the standard Emacs save shortcut globally with this alternative, add the following snippet to your configuration:
```elisp
(keymap-global-set "C-x C-s" #'buffer-guardian-save-buffer)
```
## Author and License
The *buffer-guardian* Emacs package has been written by [James Cherti](https://www.jamescherti.com/) and is distributed under terms of the GNU General Public License version 3, or, at your choice, any later version.
Copyright (C) 2026 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.
## Links
- [buffer-guardian.el @GitHub](https://github.com/jamescherti/buffer-guardian.el)
Other Emacs packages by the same author:
- [minimal-emacs.d](https://github.com/jamescherti/minimal-emacs.d): This repository hosts a minimal Emacs configuration designed to serve as a foundation for your vanilla Emacs setup and provide a solid base for an enhanced Emacs experience.
- [compile-angel.el](https://github.com/jamescherti/compile-angel.el): **Speed up Emacs!** This package guarantees that all .el files are both byte-compiled and native-compiled, which significantly speeds up Emacs.
- [outline-indent.el](https://github.com/jamescherti/outline-indent.el): An Emacs package that provides a minor mode that enables code folding and outlining based on indentation levels for various indentation-based text files, such as YAML, Python, and other indented text files.
- [easysession.el](https://github.com/jamescherti/easysession.el): Easysession is lightweight Emacs session manager that can persist and restore file editing buffers, indirect buffers/clones, Dired buffers, the tab-bar, and the Emacs frames (with or without the Emacs frames size, width, and height).
- [vim-tab-bar.el](https://github.com/jamescherti/vim-tab-bar.el): Make the Emacs tab-bar Look Like Vim’s Tab Bar.
- [elispcomp](https://github.com/jamescherti/elispcomp): A command line tool that allows compiling Elisp code directly from the terminal or from a shell script. It facilitates the generation of optimized .elc (byte-compiled) and .eln (native-compiled) files.
- [tomorrow-night-deepblue-theme.el](https://github.com/jamescherti/tomorrow-night-deepblue-theme.el): The Tomorrow Night Deepblue Emacs theme is a beautiful deep blue variant of the Tomorrow Night theme, which is renowned for its elegant color palette that is pleasing to the eyes. It features a deep blue background color that creates a calming atmosphere. The theme is also a great choice for those who miss the blue themes that were trendy a few years ago.
- [Ultyas](https://github.com/jamescherti/ultyas/): A command-line tool designed to simplify the process of converting code snippets from UltiSnips to YASnippet format.
- [dir-config.el](https://github.com/jamescherti/dir-config.el): Automatically find and evaluate .dir-config.el Elisp files to configure directory-specific settings.
- [flymake-bashate.el](https://github.com/jamescherti/flymake-bashate.el): A package that provides a Flymake backend for the bashate Bash script style checker.
- [flymake-ansible-lint.el](https://github.com/jamescherti/flymake-ansible-lint.el): An Emacs package that offers a Flymake backend for ansible-lint.
- [inhibit-mouse.el](https://github.com/jamescherti/inhibit-mouse.el): A package that disables mouse input in Emacs, offering a simpler and faster alternative to the disable-mouse package.
- [quick-sdcv.el](https://github.com/jamescherti/quick-sdcv.el): This package enables Emacs to function as an offline dictionary by using the sdcv command-line tool directly within Emacs.
- [enhanced-evil-paredit.el](https://github.com/jamescherti/enhanced-evil-paredit.el): An Emacs package that prevents parenthesis imbalance when using *evil-mode* with *paredit*. It intercepts *evil-mode* commands such as delete, change, and paste, blocking their execution if they would break the parenthetical structure.
- [stripspace.el](https://github.com/jamescherti/stripspace.el): Ensure Emacs Automatically removes trailing whitespace before saving a buffer, with an option to preserve the cursor column.
- [persist-text-scale.el](https://github.com/jamescherti/persist-text-scale.el): Ensure that all adjustments made with text-scale-increase and text-scale-decrease are persisted and restored across sessions.
- [pathaction.el](https://github.com/jamescherti/pathaction.el): Execute the pathaction command-line tool from Emacs. The pathaction command-line tool enables the execution of specific commands on targeted files or directories. Its key advantage lies in its flexibility, allowing users to handle various types of files simply by passing the file or directory as an argument to the pathaction tool. The tool uses a .pathaction.yaml rule-set file to determine which command to execute. Additionally, Jinja2 templating can be employed in the rule-set file to further customize the commands.
- [kirigami.el](https://github.com/jamescherti/kirigami.el): The *kirigami* Emacs package offers a unified interface for opening and closing folds across a diverse set of major and minor modes in Emacs, including `outline-mode`, `outline-minor-mode`, `outline-indent-minor-mode`, `org-mode`, `markdown-mode`, `vdiff-mode`, `vdiff-3way-mode`, `hs-minor-mode`, `hide-ifdef-mode`, `origami-mode`, `yafolding-mode`, `folding-mode`, and `treesit-fold-mode`. With Kirigami, folding key bindings only need to be configured **once**. After that, the same keys work consistently across all supported major and minor modes, providing a unified and predictable folding experience.