Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Emacs, is there a command to visit the most recently opened file?

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

like image 505
incandescentman Avatar asked Feb 16 '13 21:02

incandescentman


People also ask

How do I reopen a file in Emacs?

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

What is the command to searching a file from within Emacs?

To find a file in Emacs, you use the C-x C-f ( find-file ) command.

How many buffer open in Emacs at a time?

At any time, one and only one buffer is selected. It is also called the current buffer.

How do I read a file in Emacs?

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.


2 Answers

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.

like image 75
jpkotta Avatar answered Sep 18 '22 17:09

jpkotta


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.

like image 26
pokita Avatar answered Sep 17 '22 17:09

pokita