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.
(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.
(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
1 or ivy
2 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
.
(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))))
- https://emacs-helm.github.io/helm/ [return]
- https://github.com/abo-abo/swiper/ [return]
Top comments (0)