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?

El Diego Efe
  • 1,601
  • 1
  • 19
  • 24

4 Answers4

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)
  • 4
    This will restore files from the previous session, but it will not restore the window layout. – itsjeyd Oct 08 '14 at 08: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. – Sviridov Alexander Oct 08 '14 at 08: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 08 '14 at 09:20
  • Note that I am not criticizing your answer. I am simply pointing out that using `desktop-save-mode` does not address *all* of the aspects that the OP is interested in. – itsjeyd Oct 08 '14 at 09:24
  • 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. – Sviridov Alexander Oct 08 '14 at 09:29
  • I see. That's good news! I'll rephrase my comment, then: `desktop-save-mode` *might* not restore the window layout :) – itsjeyd Oct 08 '14 at 09:42
  • `desktop-save-mode` does restore the window layout IF you have an emacs with version 24.4 or higher, like the OP says – pors May 10 '15 at 17: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 03 '16 at 05:55
  • It restores *frame* layout, not *window* layout. Frame is what normal people call window. – robert Oct 12 '17 at 09:43
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

robert
  • 154
  • 6
Nsukami _
  • 6,341
  • 2
  • 22
  • 35
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.

itsjeyd
  • 14,586
  • 3
  • 58
  • 87
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.

Mr. Wacky
  • 151
  • 3