emacselispdiredrecent-file-list

in Emacs, how to maintain a list of recent directories?


In Emacs, I use recentf extensively. Rather than calling find-files, I usually call a custom function xsteve-ido-choose-from-recentf instead which allows me to choose from my recentf files.

How do I create and maintain a separate list of recent directories, separate from the list of recent files? So that instead of calling dired, I could call something like ido-choose-from-recent-directories?


Solution

  • Pragmatic Emacs found the solution.

    Here is a function to give you a list of recent directories, using ivy (part of swiper) to narrow it dynamically, and then open the selected one in dired.

    ;; open recent directory, requires ivy (part of swiper)
    ;; borrows from http://stackoverflow.com/questions/23328037/in-emacs-how-to-maintain-a-list-of-recent-directories
    (defun bjm/ivy-dired-recent-dirs ()
      "Present a list of recently used directories and open the selected one in dired"
      (interactive)
      (let ((recent-dirs
             (delete-dups
              (mapcar (lambda (file)
                        (if (file-directory-p file) file (file-name-directory file)))
                      recentf-list))))
    
        (let ((dir (ivy-read "Directory: "
                             recent-dirs
                             :re-builder #'ivy--regex
                             :sort nil
                             :initial-input nil)))
          (dired dir))))
    
    (global-set-key (kbd "C-x C-d") 'bjm/ivy-dired-recent-dirs)
    

    Source: Open a recent directory in dired: revisited | Pragmatic Emacs