20

Every time I open up Emacs I tend to set up 3 windows by doing C-x 3 (split-window-right) and then C-x2 (split-window-below). After that I have to switch to each window to open the files I want to edit.

How can I set this to be done automatically?

Is there a way to have Emacs recover the same screen organization (windows and files) from last use?

4 Answers 4

14

Since Emacs 24.4, you can use desktop-save-mode. It's been part of Emacs for ages, but the window layout is only saved and restored since version 24.4.

Just add this line in your init file:

(desktop-save-mode 1)
9
  • 4
    This will restore files from the previous session, but it will not restore the window layout.
    – itsjeyd
    Oct 8, 2014 at 8:16
  • 1
    Nope, I've tested it. Emacs Manual: Once you save the Emacs desktop — the buffers, their file names, major modes, buffer positions, and so on — then subsequent Emacs sessions reload the saved desktop. Oct 8, 2014 at 8:21
  • 4
    "Buffer position" refers to the position of point (the cursor) in a given buffer. Yes, this will be restored with desktop-save-mode. Window layout refers to how many windows are currently displayed and how they are positioned. desktop-save-mode does not preserve this layout.
    – itsjeyd
    Oct 8, 2014 at 9:20
  • 3
    Yes, "window layout" and "buffer position" are different things, you are correct. But my window layout was restored. It looks like saving of window layout was added in emacs-snapshot. Oct 8, 2014 at 9:29
  • 1
    @pors, it seems not the case if you are using Emacs (24.4 or higher) in terminal mode. No window configuration is saved/restored.
    – skyork
    Jan 3, 2016 at 5:55
8

You can use workgroups2 to manage your desktop. From the README on GitHub:

Workgroups is a session manager for Emacs.

  • It saves all your opened buffers, their location and sizes on disk to restore later
  • You can create several workspaces

You can also restore such buffers as: org-agenda, shell, magit-status, help.

If you have the MELPA repository enabled, you can install this package via M-x package-install RET workgroups2 RET.

The basic setup is:

(require 'workgroups2)
(workgroups-mode 1) ; This should go at the end of your init file

When you restart Emacs for the first time after adding this code to your init file, workgroups2 will automatically create a workgroup for you. You can verify that this was successful by checking the *Messages* buffer for the following information:

Workgroups Mode: on
Switched: First workgroup
Created: First workgroup  ( -<{ 0: First workgroup }>- )

From this point on, Emacs will save the current configuration of windows and files automatically on exit, and restore it the next time it starts.

8

write a function close to that one in your init file, you will probably have to modify it a little bit to fit your needs:

;; layout definition
(defun my-startup-layout ()
 (interactive)
 (delete-other-windows)
 (split-window-horizontally) ;; -> |
 (next-multiframe-window)
 (find-file "~/.emacs.d/init.el")
 (split-window-vertically) ;;  -> --
 (next-multiframe-window)
 (find-file "~/.emacs.d/init_settings.el")
 (next-multiframe-window)
 (dired "~")
)

;; execute the layout
(my-startup-layout )

is there a way to have Emacs recover the same screen organization (windows and files) from last use?

You should have a look at the layout package

3

I've been using something modeled after sanityinc's .emacs.d. It uses desktop-save-mode, and also is smart enough to use frame-restore on older versions emacs. From the readme for frame-restore from package-list-packages:

Save and restore parameters of Emacs frames.

Just call frame-restore' in yourinit.el':

(frame-restore-mode)

Note that since r113242 the built-in Desktop Save mode will restore frames. If you are using a Emacs snapshot build later than this revision, you are strongly advised to use Desktop Save mode instead:

(desktop-save-mode)

Frame Restore mode will display a bold warning if enabled in an Emacs build whose Desktop Save mode can restore frames.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.