Sometimes my ubuntu freezes, and I am using Emacs that has many files opened inside it. I have to reboot Ubuntu, and lose the track of what files I have opened.

Is there a way to periodically record the files being opened in Emacs (maybe by more than one Emacs process), and to allow me to read the record of opened files later for example after rebooting OS?

Btw, I found that sudo lsof -u t | grep emacs only gives the directory where each emacs process was started, not the files being opened in Emacs. Does anyone know why?

  • re the lsof, I think it's because emacs doesn't keep the files open, it just reads it into a buffer and then closes the file handle.
    – npostavs
    Commented Apr 2, 2018 at 21:35

3 Answers 3


You can use Desktop mode for this, see (emacs) Saving Emacs Sessions:

Use the desktop library to save the state of Emacs from one session to another. 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.


When desktop-save-mode is active and the desktop file exists, Emacs auto-saves it every desktop-auto-save-timeout seconds, if that is non-nil and non-zero.

  • Thanks. Does your way help to reopen files after rebooting Ubuntu?
    – Tim
    Commented Apr 18, 2018 at 17:59
  • @Tim Yes, the desktop file is a persistent file on disk, so it survives across reboots.
    – npostavs
    Commented Apr 18, 2018 at 18:46

Create an idle-timer whose function maps over buffer-list gathering the buffer-file-names and writing them to a file.

Turn on that timer in each Emacs session. You probably don't want to run the timer function too often.

Keep track of the file names written, so you can avoid rewriting the same set of names to disk. (For that, make sure you check for set, not list, equality.)

  • Thanks, but I am not familiar with elisp... and I don't know how to do what you said. I'd appreciate If you could provide specific code and steps.
    – Tim
    Commented Apr 2, 2018 at 4:04
  • It's not hard. But I'll leave that to someone else's answer. If no one answers with such code then I'll post something later, when I get a moment. But I expect that you'll see a good answer soon from someone else.
    – Drew
    Commented Apr 2, 2018 at 13:41

Install supersave and enable idle saving, e.g.:

(use-package super-save
  :ensure t
  :init (setq super-save-auto-save-when-idle t)
  :config (super-save-mode +1))
  • 1
    Can you elaborate a bit about what it does? Does it save all file-visiting buffers, for example?
    – Drew
    Commented Apr 2, 2018 at 19:29
  • My answer is off topic. The correct answer is the desktop-mode. Supersafe saves all file visiting buffers every time a buffer loses focus and can be made to save when emacs is idle, but it does not keep a list of files.
    – Heikki
    Commented Apr 4, 2018 at 7:07
  • If you think your answer is helpful in the context of this question then please consider incorporating some of your comment into the answer. If you think it is not helpful here, please consider deleting the answer. Thx.
    – Drew
    Commented Apr 4, 2018 at 14:58

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.