2

I've seen a tip several times that ido-file-extensions-order can be used to easily order files. Setup is straightforward (code at bottom), but these filetypes are only given preference when choosing between two otherwise identical files. Is there a way to order files based on type in the minibuffer (or any way other than alphabeticaly?).

(setq ido-file-extensions-order '(".org" ".py" ".txt" ".cpp" ".tex")
      ido-enable-flex-matching t)
(ido-mode 1)
  • ido-file-extensions-order variable is used to customize the order in which files are sorted when Ido displays them in the minibuffer. Isn't there a difference in the minibuffer when you try to open a file with and without this setting? – Nsukami _ Oct 22 '15 at 2:04
  • 1
    @Nsukami_ Thank you, I just realized that there is a large discrepancy between what I thought that variable would do, and what it does. (the webpages I've read, eg here incorrectly state it sorts the order. It actually gives preference to filetypes when there are two otherwise identical files.) – user2699 Oct 22 '15 at 3:30
1

This code on the emacswiki allows a custom sort order, based on modification time. I've modified it to sort based on file extensions in ido-file-extensions-order.

;; sort ido filelist by extension instead of alphabetically
(require 'cl)
(defun ido-sort-extension ()
  " sort ido file names by extension "
  (setq ido-temp-list
        (sort ido-temp-list 
              (lambda (a b)
                (< (order-file-extension a)
                   (order-file-extension b)))
              ))
  (ido-to-end  ;; move . files to end (again)
   (delq nil (mapcar
              (lambda (x) (and (char-equal (string-to-char x) ?.) x))
              ido-temp-list))))

(defun order-file-extension (name)
  " helper function to sort files by extension with ido"
  (or
   (and (file-directory-p name) -1) ;list directories first
   (cl-position t (mapcar (lambda (str)
                            (string= str (concat "." (file-name-extension name))))
                          ido-file-extensions-order))
   (+ 1 (length ido-file-extensions-order))))
(add-hook 'ido-make-file-list-hook 'ido-sort-extension)

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.