Save and Restore Window Configuration in Emacs
Tags: emacs, elisp
Published: 2019-05-17 01:21:27

---
title: Save and Restore Window Configuration in Emacs
published: true
tags: emacs, elisp
canonical_url: https://erick.navarro.io/blog/save-and-restore-window-configuration-in-emacs/
---

When we are using `emacs` sometimes we often open many buffers in different windows at the same time, for example we can have a buffer for a opened file, another for seeing test results and so on.

There are some tools to manage "sessions" but I wanted something simple and I also wanted to learn a little bit more of `elisp` so here is the result.

```emacs-lisp
(defvar window-snapshots '())

  (defun save-window-snapshot ()
    "Save the current window configuration into `window-snapshots` alist."
    (interactive)
    (let ((key (read-string "Enter a name for the snapshot: ")))
      (setf (alist-get key window-snapshots) (current-window-configuration))
      (message "%s window snapshot saved!" key)))

  (defun get-window-snapshot (key)
    "Given a KEY return the saved value in `window-snapshots` alist."
    (let ((value (assoc key window-snapshots)))
      (cdr value)))

  (defun restore-window-snapshot ()
    "Restore a window snapshot from the window-snapshots alist."
    (interactive)
    (let* ((snapshot-name (completing-read "Choose snapshot: " (mapcar #'car window-snapshots)))
           (snapshot (get-window-snapshot snapshot-name)))
      (if snapshot
          (set-window-configuration snapshot)
        (message "Snapshot %s not found" snapshot-name))))
```

The code basically do 3 things:

- Define an `alist` where window configurations will be saved
- Save the current window configuration using a name to be identified later.
- Restore any of the saved configurations by selecting it from a list

Now let's get into the details:

## Save the current window configuration

We will use the function `current-window-configuration` to get the current state of the windows and put this value in an `alist` called `window-snapshots`. Also it should asks for a name so we can search for it later.

```emacs-lisp
(defvar window-snapshots '())

  (defun save-window-snapshot ()
    "Save the current window configuration into `window-snapshots` alist."
    (interactive)
    (let ((key (read-string "Enter a name for the snapshot: ")))
      (setf (alist-get key window-snapshots) (current-window-configuration))
      (message "%s window snapshot saved!" key)))
```

## Restore a window configuration

We will use `completing-read` to select one of the saved snapshots from an interactive list. It will use `helm` <sup id="fnref:helm"><a href="#fn:helm">1</a></sup> or `ivy` <sup id="fnref:ivy"><a href="#fn:ivy">2</a></sup> if any of those are installed otherwise it will show the options in the `minibuffer`.

This will show the `name` used before and retrieve the value of the window configuration. Then it will apply the configuration using `set-window-configuration`.

```emacs-lisp
(defun get-window-snapshot (key)
    "Given a KEY return the saved value in `window-snapshots` alist."
    (let ((value (assoc key window-snapshots)))
      (cdr value)))

  (defun restore-window-snapshot ()
    "Restore a window snapshot from the window-snapshots alist."
    (interactive)
    (let* ((snapshot-name (completing-read "Choose snapshot: " (mapcar #'car window-snapshots)))
           (snapshot (get-window-snapshot snapshot-name)))
      (if snapshot
          (set-window-configuration snapshot)
        (message "Snapshot %s not found" snapshot-name))))
```

* * *

1. https://emacs-helm.github.io/helm/ <sup>[return]</sup>
2. https://github.com/abo-abo/swiper/ <sup>[return]</sup>