Skip to content

Find files faster with the recent files package

by mickey on January 27th, 2011

I bet the majority of files you edit on a day-to-day basis are the same ones over, and over again. For that reason I recommend you use Emacs’s recentf package, it is a great — and very sophisticated, like all things Emacs — utility that keeps track of recently used files.

I supercharge recentf by adding Ido mode support (if you don’t know what Ido is, read Introduction to Ido Mode); and by overriding C-x C-r, bound to find-file-read-only, a useless feature I never use. Note: the Ido supercharging only works if you have ido-mode enabled in the first place!

Note that unlike find-file I’ve opted to display the entire filepath in Ido’s completion engine as I often find that a directory or remote host is the only disambiguator if there are multiple files with the same name. I’ve thought about filtering the list of recent files through the uniquify module for buffers but that’s for another time.

Here’s what you need to add to your .emacs:

  1. seagle permalink

    I prefer to this way. Simple and work well.

    (recentf-mode 1) ; recentf
    (defun recentf-open-files-compl ()
    (let* ((all-files recentf-list)
    (tocpl (mapcar (function
    (lambda (x) (cons (file-name-nondirectory x) x))) all-files))
    (prompt (append ‘(“File name: “) tocpl))
    (fname (completing-read (car prompt) (cdr prompt) nil nil)))
    (find-file (cdr (assoc-string fname tocpl)))))
    (global-set-key [(control x)(control r)] ‘recentf-open-files-compl)

    • mickey permalink

      I prefer ido-completing-read by a long shot as the fuzzy-matching and filter-as-you-type completion makes finding things a lot easier.

  2. I found a slightly different version on the web some years ago. It’s very similar except that it replaces your $HOME path by ~:

    (defun xsteve-ido-choose-from-recentf ()
    "Use ido to select a recently opened file from the `recentf-list'"
    (let ((home (expand-file-name (getenv "HOME"))))
    (ido-completing-read "Recentf open: "
    (mapcar (lambda (path)
    (replace-regexp-in-string home "~" path))
    nil t))))

  3. tom permalink

    Not as fancy but if I anticipate using the same file over and over again I stick it in the bookmarks – a feature I knew about for a while but never really used until recently.

    What I particularly like is that I can bookmark dired directories and jump to them straight away rather navigating through lots of subdirectories.

    • Scott permalink

      @tom and there’s some code on emacswiki to make bookmarks usable from your shell so you can cp foo $bm/bar or cd $bm

    • mickey permalink

      Yeah, bookmarks are great but the beauty of recentf is that files I use often are naturally pushed to the top of my 50-item list of recent files, whereas the ones I use frequently (or not at all) after a while will slowly drop off the list. I’ve found that by the time a file drops off the list I’m probably not using it often enough to warrant a “shortcut.”


  4. rcy permalink

    This is Great. Thanks.

    One thing. I have ignored ido mode for a long time, being happy with my trusty iswitchb-mode. I pasted your code above into my .emacs, evalled, and hit C-x C-r. Completion did not work. I needed to hit C-] to escape from the minibuffer.

    Adding (ido-mode t) to my .emacs solved the problem.

    Just wanted to point that out, in case other users not (yet) using ido try your snippet.


    • mickey permalink

      Ahh, yes. Excellent observation. I suppose I should point that out. I do recommend you read my article Introduction to Ido Mode. I think you’ll find it useful.

  5. visitor permalink

    There is yet another approach which provides normal file opening while oferring completions from the recentf list at the same time:

  6. There is something in ido-mode that does something similar already. Press C-x C-f, type the file name (even though you’re in the wrong directory), wait a second and it pops up the correct directory (it has its own cache separate from recentf).

    Of course, this version might still work better.

  7. Paul Rodriguez permalink

    I used to use a similar Ido-based solution:

    “Recent file: ”
    (mapcar ‘abbreviate-file-name recentf-list)
    nil t))

    But now I just use `recentf-open-files’, which is even more convenient.

  8. iceman permalink

    i think i didn’t need the ido-recentf-open,because ido will do this,when i typing some filename under very deep directory,ido will auto match it,if i have recently open that file?isn’t this a convetion?

  9. Is there a way to make it so the list of recent files does NOT show the filepath, but just the filename itself?

  10. I went ahead and implemented behaviour similar to uniquify for the recentf-list, so as to make it more readable. Hopefully it’ll be helpful to other people as well:

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS