Conveniently save and restore frame configurations

Posted: November 26, 2006 in Emacs

In emacs you can split each frame in several windows. Such a configuration is called window configuration. The window configurations of all frames make a frame configuration. Here are some functions which let you save and restore such a frame configuration with only one key.
First we define a register which will be used by default for saving the frame configuration:

(defparameter th-frame-config-register ?°
  "The register which is used for storing and restoring frame
configurations by `th-save-frame-configuration' and
`th-jump-to-register'.")

The next thing is the saving function. If you call it with a prefix arg you can choose a different register:

(defun th-save-frame-configuration (arg)
  "Stores the current frame configuration in register
`th-frame-config-register'. If a prefix argument is given, you
can choose which register to use."
  (interactive "P")
  (let ((register (if arg
                      (read-char "Which register? ")
                    th-frame-config-register)))
    (frame-configuration-to-register register)
    (message "Frame configuration saved in register '%c'."
             register)))

Now we need a function to restore a frame configuration. By default it uses th-frame-config-register, but with a prefix arg you can choose any register. (You can use this function not only for restoring frame configs, but for everything you can do with jump-to-register…)

(defun th-jump-to-register (arg)
  "Jumps to register `th-frame-config-register'. If a prefix
argument is given, you can choose which register to jump to."
  (interactive "P")
  (let ((register (if arg
                      (read-char "Which register? ")
                    th-frame-config-register)))
    (jump-to-register register)
    (message "Jumped to register '%c'."
             register)))

Ok, the last thing we gotta do is create some key bindings. I chose F5 and F6:

(global-set-key (kbd "<F5>")
                'th-save-frame-configuration)
(global-set-key (kbd "<F6>")
                'th-jump-to-register)

Now whenever you have a complex window/frame config press F5 to save it and press F6 to restore it.

Comments
  1. […] blog entry Gonveniently save and restore frame configurations is related. Filed under: Emacs […]

  2. Anupam says:

    have you looked tried winner-mode ? [activate it with the usual M-x winner-mode]. it does what you have mentioned, but the difference being, that once it is active, you can go forwards/backwards in window-configuration history. i am not sure how “deep” the history buffer is though.

  3. Tassilo Horn says:

    Hi Anupam,

    yes, I know winner-mode, but I like my functions better, because most of the time I use a quite complex layout (gnus on the left and 3 or 4 irc buffers on the right).

    Bye,
    Tassilo

  4. […] I saved the layout and can quickly restore it. See my blog entry Conveniently save and restore frame configurations […]

  5. Zoran Rilak says:

    What’s wrong with C-x r w / C-x r j ?

    • Tassilo Horn says:

      Nothing’s wrong with them. It’s just that in most cases I use only one window/frame configuration, and in that case it’s one against four keys.

  6. Shashank says:

    Thanks for your functions!

    Is there a way to persist these register saves?

  7. Phil says:

    It seems to fail early on here (Emacs 23.3, OSX):
    Symbol’s function definition is void: defparameter

    • Tassilo Horn says:

      Oh, indeed. There is no standard defparameter. Here’s my definition:

      (defmacro defparameter (symbol &optional initvalue docstring)
      "Common Lisp's defparameter."
      `(progn
      (defvar ,symbol nil ,docstring)
      (setq ,symbol ,initvalue)))

  8. Phil says:

    Ah, yes, thank!
    I just changed ” and ” to ” and ” to make it work on OS X. (Weird that it’s lower case?)

    Would be nice to have it persistent, though. I would love to have two different standard window layouts to pull up at need …
    (It would be nice to understand some more [e]lisp to follow that thread you mention.) Maybe I’ll look more into that after my current Python learning project is over … :)

  9. Phil says:

    eh, I changed ‘F5’ and ‘F6’ to ‘f5’ and ‘f6’ …
    (got lost in parsing in my previous comment)

Leave a reply to Zoran Rilak Cancel reply