4

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

2

"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
systems."
  (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)))
  • 1
    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 May 29 '15 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. – Joe Corneli May 29 '15 at 13:20
  • @T.Verron I've replaced the old patch with a more user-friendly snippet of lisp code. – Joe Corneli Sep 27 '18 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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