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
).
Open an existing file by entering emacs in the shell followed by the name of the file. This will default to running Emacs in a GUI, but it can also be run within the shell ( emacs -nw ).
To find a file in Emacs, you use the C-x C-f ( find-file ) command.
At any time, one and only one buffer is selected. It is also called the current buffer.
To read a disk file into an emacs buffer, type the command Control-X-Control-F. Emacs will ask you for the name of the file. As you type the name of the file, it will be displayed in the mini buffer.
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.
You can do this by using the recentf package:
(require 'recentf)
(defvar my-recentf-counter 0)
(global-set-key (kbd "<M-up>")
(lambda ()
(interactive)
(setq my-recentf-counter (1+ my-recentf-counter))
(recentf-open-most-recent-file my-recentf-counter)))
You will probably face the problem that there are files in the "recent" list which you are not interested in (temporary stuff that was saved upon exiting Emacs). Use the recentf-exclude variable to exclude those files from the list of recently opened files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With