Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs: reopen buffers from last session on startup?

Tags:

emacs

Every day I start up emacs and open the exact same files I had open the day before. Is there something I can add to init.el file so it will reopen all the buffers I was using when I last quit emacs?

like image 647
kristina Avatar asked Apr 29 '09 19:04

kristina


4 Answers

You can use the Emacs Desktop library:

You can save the desktop manually with the command M-x desktop-save. You can also enable automatic saving of the desktop when you exit Emacs, and automatic restoration of the last saved desktop when Emacs starts: use the Customization buffer (see Easy Customization) to set desktop-save-mode to t for future sessions, or add this line in your ~/.emacs file:

 (desktop-save-mode 1)
like image 89
Jared Oberhaus Avatar answered Oct 13 '22 22:10

Jared Oberhaus


Although I suspect the question was looking for the emacs "desktop" functionality (see above answer), Lewap's approach can be useful if the set of files one uses really is the exact same file set. In fact, one can go a step further and define 'profiles' if one has different sets of regularly used files... Quickie example:

(let ((profile 
       (read-from-minibuffer "Choose a profile (acad,dist,lisp,comp,rpg): ")
       ))
  (cond
   ((string-match "acad" profile) 
    (dired "/home/thomp/acad")
    (dired "/home/thomp/acad/papers")
    )
   ((string-match "lisp" profile)
    (setup-slime)
    (lisp-miscellany)
    (open-lisp-dirs)
    )
   ((string-match "rpg" profile)
    (find-file "/home/thomp/comp/lisp/rp-geneval/README")
    (dired "/home/thomp/comp/lisp/rp-geneval/rp-geneval")
... etc.

If you find that you regularly switch back and forth between different sets of regularly-used files as you work, consider using perspectives and populating each perspective with the desired set of regularly-used files.

like image 27
dat Avatar answered Oct 13 '22 21:10

dat


For storing/restoring the buffers/tabs (specifically elscreen tabs): I use elscreen and the way I manage storing/restoring the desktop session and the elscreen tab configuration is the following code in my .emacs file (the names used are self-explanatory and if the storing/restoring functions should not be executed every time emacs starts just comment out the lines with "(push #'elscreen-store kill-emacs-hook)" and "(elscreen-restore)"):

(defvar emacs-configuration-directory
    "~/.emacs.d/"
    "The directory where the emacs configuration files are stored.")

(defvar elscreen-tab-configuration-store-filename
    (concat emacs-configuration-directory ".elscreen")
    "The file where the elscreen tab configuration is stored.")

(defun elscreen-store ()
    "Store the elscreen tab configuration."
    (interactive)
    (if (desktop-save emacs-configuration-directory)
        (with-temp-file elscreen-tab-configuration-store-filename
            (insert (prin1-to-string (elscreen-get-screen-to-name-alist))))))
(push #'elscreen-store kill-emacs-hook)
(defun elscreen-restore ()
    "Restore the elscreen tab configuration."
    (interactive)
    (if (desktop-read)
        (let ((screens (reverse
                        (read
                         (with-temp-buffer
                          (insert-file-contents elscreen-tab-configuration-store-filename)
                          (buffer-string))))))
            (while screens
                (setq screen (car (car screens)))
                (setq buffers (split-string (cdr (car screens)) ":"))
                (if (eq screen 0)
                    (switch-to-buffer (car buffers))
                    (elscreen-find-and-goto-by-buffer (car buffers) t t))
                (while (cdr buffers)
                    (switch-to-buffer-other-window (car (cdr buffers)))
                    (setq buffers (cdr buffers)))
                (setq screens (cdr screens))))))
(elscreen-restore)
like image 6
sadesyllas Avatar answered Oct 13 '22 22:10

sadesyllas


There are useful enhancements you can make to the basic desktop feature. Particular handy (IMO) are methods of auto-saving the desktop during the session, as otherwise if your system crashes you will be stuck with the desktop file you had started that session with -- pretty annoying if you tend to keep Emacs running for many days at a time.

http://www.emacswiki.org/emacs/DeskTop

The wiki also has useful information about persisting data between sessions in general:

http://www.emacswiki.org/emacs/SessionManagement

For desktops specifically, I thought that Desktop Recover looked particularly promising, however I've not yet tried it out.

like image 2
phils Avatar answered Oct 13 '22 21:10

phils