Frage

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).

War es hilfreich?

Lösung

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.

Andere Tipps

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top