In Emacs, is there a command to open the most recently opened file? Like a visit-most-recent-file
?
Note that I don't want to view a LIST of recent files and then select one. I want to automatically visit the most recently opened one.
It would be great to visit a recently-opened file (that's no longer in a current buffer) with a single keystroke. And then be able to visit the next most recently opened file by invoking the keystroke again, and so on.
So that by pressing that keystroke four times (e.g. A-UP
), I could automatically open the top four files on my recent files list.
I can kind of sort of do this already by browsing through recent files, by pressing C-x C-f UP RET
, but it would be cool to do it in a single keystroke (e.g. A-UP
).
Building off of pokita's answer,
(defun visit-most-recent-file ()
"Visits the most recently open file in `recentf-list' that is not already being visited."
(interactive)
(let ((buffer-file-name-list (mapcar 'buffer-file-name (buffer-list)))
most-recent-filename)
(dolist (filename recentf-list)
(unless (memq filename buffer-file-name-list)
(setq most-recent-filename filename)
(return)))
(find-file most-recent-filename)))
I haven't tested this very much.