I know there is such a variable as helm-ff-initial-sort-method. However, it does not include KIND as a sorting method. Anybody know how to sort files in helm-find-file by KIND? Thank you!

  • Not sure how to get a file's KIND, by extension? is it good enough? I notice macOS's Finder can distinguish plain text and unix executable even both of them do not have extension. helm-find-files's sorting is done via helm-list-directory. – xuchunyang Sep 15 at 10:30
  • Yes, KIND is the extension. I did not understand your point regarding helm-list-directory. My goal is to just sort the files using helm-find-file by kind (or extension). Basically, having all folders on top and everything else below. – Emmanuel Goldstein Sep 16 at 2:13

Here is the code to add Shift-F4 to sort by kind:

(define-advice helm-list-directory (:override (directory) add-sort-by-kind)
  (let ((files (directory-files directory t directory-files-no-dot-files-regexp)))
    (pcase helm-ff-initial-sort-method
       (sort files #'file-newer-than-file-p))
       (sort files #'helm-ff-file-larger-that-file-p))
       (let-alist (seq-group-by #'file-directory-p files)
          .t                            ; folders
           .nil                         ; files
           (lambda (f1 f2)
             (let ((ext1 (or (file-name-extension f1) ""))
                   (ext2 (or (file-name-extension f2) "")))
               (if (equal ext1 ext2)
                   (string< f1 f2)
                 (string< ext1 ext2)))))))))))

(defun helm-ff-sort-by-kind ()
  (unless (eq helm-ff-initial-sort-method 'kind)
    (setq helm-ff-initial-sort-method 'kind)
    (helm-force-update (helm-get-selection nil helm-ff-transformer-show-only-basename)))
  (message "Sorting by kind"))
(put 'helm-ff-sort-by-kind 'helm-only t)

(define-key helm-find-files-map (kbd "S-<f4>") #'helm-ff-sort-by-kind)

Change helm-ff-initial-sort-method does not work for me, e.g., (setq helm-ff-initial-sort-method 'size), that is, the initial sort is still by alpha instead of by size, I am not sure if I understand the user option correctly. However, Shift-F1/F2/F3 work.

| improve this answer | |
  • Thanks for the help! Is "add-sort-by-kind" a variable that I should already have in my system? I can't find it. – Emmanuel Goldstein Sep 17 at 4:57
  • @EmmanuelGoldstein Nope, it's not a variable name, it's the NAME argument of define-advice, type C-h f define-advice to see its meaning. – xuchunyang Sep 17 at 6:41
  • Thanks, newbie with elisp. I added the function to my init. Evaluated it as I have done with others, but I can't find M-x helm-ff-sort-by-kind. Any ideas? – Emmanuel Goldstein Sep 17 at 18:52
  • The command does not make sense outside a helm-find-files session. You need to use the keybinding Shift-F4 while helm-find-files is running. The reason why the command does not appear in helm-M-x (I assume your M-x is helm-M-x) is probably filtered by helm-M-x. Since you're new to Emacs Lisp, the above code might be difficult to understand, I suggest don't put it to your init file. You can consider the feature you want is not supported by helm-find-files. Maybe try dired with GNU ls's -X option: -X sort alphabetically by entry extension. – xuchunyang Sep 18 at 20:22
  • Learning a lot, thanks! Will try this. One question though: I did not get how ot use dired with GNU ls -X option. What exactly shall I type on my emacs? Thanks. – Emmanuel Goldstein Sep 19 at 9:44

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.