How to sort file list in helm-find-files asks how to sort the file list so that directories float to the top. My question is different:

Is it possible to sort the files returned by helm-find-files so that they appear sorted by modification time, rather than alphabetical order (i.e., as if they had been listed by ls -lt rather than ls -la)?

1 Answer 1


"Yes -- use the source."

With a little help from StackOverflow | Emacs — sort list of directories / files by modification date... Here's a modified version of helm-ff-directory-files that does the job. Note that you should (require 'files) to get this working.

(defun helm-ff-directory-files (directory)
  "List contents of DIRECTORY.
Argument FULL mean absolute path.
It is same as `directory-files' but always returns the
dotted filename '.' and '..' even on root directories in Windows
  (setq directory (file-name-as-directory
                   (expand-file-name directory)))
  (let* (file-error
         (ls   (mapcar #'car
                       (sort (directory-files-and-attributes
                              directory 'full
                              directory-files-no-dot-files-regexp t)
                             #'(lambda (x y)
                                 (not (time-less-p (nth 6 x) (nth 6 y)))))))
         (dot  (concat directory "."))
         (dot2 (concat directory "..")))
    (puthash directory (+ (length ls) 2) helm-ff--directory-files-hash)
    (append (and (not file-error) (list dot dot2)) ls)))
  • 2
    This answer does not look too useful to a future visitor. What are you suggesting to do with this code? Patch the package source with it? This is not very user-friendly, and you will have to do it every time the package is updated.
    – T. Verron
    Commented May 29, 2015 at 13:01
  • Thanks for the comment. It motivated me to raise this issue: helm-emacs #1041 My opinion is that future visitors should learn how to use the source, and that this answer is instructive in that regard. I don't mean to imply that this patch should be maintained forever, just that it shows how to address the question. A more complicated answer would make this behavior an option, and provide nicer integration. This answer is a proof-of-concept. I think it is up to the package maintainer to make an easy-to-use version, if that desired. Commented May 29, 2015 at 13:20
  • @T.Verron I've replaced the old patch with a more user-friendly snippet of lisp code. Commented Sep 27, 2018 at 10:45
  • Wooud it be work if I just past it into the .emacs file , I am having following warning message: reference to free variable ‘helm-ff--directory-files-hash@Joe Corneli
    – alper
    Commented May 3, 2020 at 10:40
  • I think this is now supported by mainline helm. Check issue number 1041 above. Commented May 3, 2020 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.