ChatGPT解决这个技术问题 Extra ChatGPT

Emacs: reopen buffers from last session on startup?

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?

Seems that desktop handling is different in Emacs 21 and 22+. The topic is described on this page: emacswiki.org/emacs/DeskTop .
Another change in desktop handling default path came in 24.3. github.com/jwiegley/emacs-release/blob/…

J
Jared Oberhaus

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)


Great answer. However, I noticed that Emacs rearranges the buffer order between sessions. Is there any way to always have the same buffer order?
If anyone has problems getting this to work (like I did), here's a technique to diagnose the issue: turn on debugging by M-: executing (setq debug-on-error t), then call desktop-revert and it might catch the offending error in the debugger. For me it was global setting that was supposed to be buffer local causing frame restoration to fail. (Just using emacs --debug-init might catch the issue too, but I used former method.)
If you're wondering, like me: Manual restoration is triggered by M-x desktop-read
d
dat

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.


s
sadesyllas

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)

For anyone going this route, just a minor point that you can replace emacs-configuration-directory with user-emacs-directory which Emacs already provides.
p
phils

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.


y
yPhil

(find-file-noselect "/my/file") will open it silently, ie w/o raising the buffer. Just saying.

EDIT This command is not interactive ; To test it you have to evaluate the expression, for example by positioning the cursor after the last parenthesis and hitting C-x C-e

Downvoting this is not cool ; this command definitely works and is in the scope of the question.


I didn't downvote, but perhaps your answer was downvoted because it does not address the issue of how to open specifically the buffers that were open in the previous Emacs session.
downvoted because it has nothing to do with the original Q.