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?
1 Answer
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.
-
You should call
add-hook
with theappend
argument set tot
to get your hook called at the very end of the initialization hooks. Maybe, some other library also adds stuff there.– TobiasCommented 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.– RezaCommented 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.– RezaCommented Apr 3, 2016 at 0:42
-
This could help.
(add-to-list 'recentf-exclude "recentf")
Commented May 31, 2016 at 19:47
(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 otherrequire
for that library before. I.e., a(assert (null (featurep 'save-visited-files)))
at this point must not quarrel.after-init-hook
as in the answer of Swarnendu Biswas.