6

I have save-place and save-visited-files installed however whenever I start emacs first a directory list shows (result of another setting in .emacs), I close it and then a few message buffers show up and it's after closing all of them that I get to the file I was working on last time. How can I configure emacs to show the file from last session when started?

3
  • Just (require 'save-visited-files) as last thing in your init file (especially after your directory list stuff) should do the trick. Note, that there must not be any other require for that library before. I.e., a (assert (null (featurep 'save-visited-files))) at this point must not quarrel.
    – Tobias
    Commented Apr 1, 2016 at 15:40
  • The method from my last comment may fail if the directory list stuff plays around with after-init-hook as in the answer of Swarnendu Biswas.
    – Tobias
    Commented Apr 1, 2016 at 15:51
  • 3
    In addition to the answers given so far, if you want to restore more than just the last file you were editing, you can save and restore your Emacs desktop. See the Emacs manual, node Saving Emacs Sessions.
    – Drew
    Commented Apr 1, 2016 at 15:58

1 Answer 1

1

Add the following snippet to your init file to open the last file you were working on:

(require 'recentf)
(recentf-mode 1)
(add-hook 'after-init-hook
           (lambda ()
              (recentf-open-most-recent-file 1)) t)

This enables recentf, which tracks the list of recent files you have opened. Then, you instruct Emacs to open the most recent file after initialization.

Note: If you use the sync-recentf package, then on starting Emacs, your first item will probably be /tmp/sync-recentf-marker. In that case, you might want to open the second-most recent file, or use a conditional checking for the file name.

If you always want to open a fixed file, then you can also set initial-buffer-choice.

Update: Added an optional third argument to the argument list of add-hook in order to append the hook to the end of the initialization based on suggestions from Tobias.

4
  • You should call add-hook with the append argument set to t to get your hook called at the very end of the initialization hooks. Maybe, some other library also adds stuff there.
    – Tobias
    Commented Apr 1, 2016 at 15:49
  • Unfortunately it didn't help. First I get "Welcome to emacs" then a log then scratch then the messages and its after closing these that I get the file I was working on last time.
    – Reza
    Commented Apr 3, 2016 at 0:33
  • Installed 11.88 using ELPA but Auctex doesn't show up when opening a TeX file. I uninstalled it.
    – Reza
    Commented Apr 3, 2016 at 0:42
  • This could help. (add-to-list 'recentf-exclude "recentf") Commented May 31, 2016 at 19:47

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.