5.9 KiB
Emacs Persist State
- Introduction
- Supported packages
- Installation and usage
- Configuration
- Multiple Emacs instances
- Related packages
- Contact
Introduction
I don't leave Emacs often, but sometimes Emacs leaves me.
At moments like these it's convenient to start Emacs again with a reasonably up to date state from the previous session: history variables, bookmarks, open buffers, etc. Usually these items are saved at Emacs exit (as part of kill-emacs-hook
), but when Emacs exits unexpectedly due to a crash, these functions are not executed and you're left with an outdated state (if any).
This package saves your Emacs state regularly when idle for a brief moment, to minimize the disturbance of executing all save functions in sequence (as Emacs blocks for a brief moment). In case Emacs is idle for a longer time, no state is saved.
Supported packages
persist-state supports a number of packages out of the box. For some packages the save only takes place when the corresponding mode or variable is set accordingly. For example, saving history only takes place if savehist-mode
is active. Consult the package's documentation or source code for more info about auto-saving.
- bookmark
- desktop
- Eshell history
- Prescient.el
- recentf
- savehist
- saveplace
Installation and usage
The easiest method is to install via MELPA.
Alternatively, clone the repository and put the package in your load-path
, for example with use-package
:
git clone https://codeberg.org/bram85/emacs-persist-state.git
(use-package persist-state
:ensure nil
:load-path "/path/to/emacs-persist-state"
:config
(persist-state-mode))
Configuration
This table shows which variables can be customized.
Custom variable | Description |
---|---|
persist-state-save-interval | Interval (in seconds) to persist state. |
persist-state-saving-functions | A list of functions that should be executed as part of saving state. |
persist-state-wait-idle | When it's time to save, wait for this amount of idle time (in seconds). |
To add your own save functions, simply add those to the persist-state-functions
variable. For example, if you use the org-visibility package, you could save the heading visibility data on a regular basis as well:
(add-to-list 'persist-state-functions #'org-visibility-save-all-buffers)
Multiple Emacs instances
persist-state doesn't do anything special regarding multiple Emacs instances, it relies on the persistence capabilities of other packages. So persist-state only supports multiple instances if other packages do. However, most of them don't, so whoever writes last 'wins'.
Actually, this is not any different than saving with the emacs-kill-hook
, as most supported packages do. The state of the last Emacs instance will be persisted. The difference with persist-state is that state is persisted while running Emacs instances, not only at the exit.
For the use-case where you have one main instance, you could detect whether the server is running. Other instances wouldn't spawn a new server, so the main instance is detectable through a non-nil server-process
variable. For example, you could use the following configuration to trigger persist-state for the main instance only:
(use-package persist-state
:after server
:if server-process
:config
(persist-state-mode))
Related packages
- persist
- This package targets package developers to declare variables to be persisted across Emacs sessions.
- savehist
- This built-in package saves history variables. Actually, this package is called by persist-state to save the history variables at regular intervals.
Contact
- Issue reports: Codeberg
- Mastodon: @bram85@emacs.ch
- Mail: see
git log
.