Mac – how to get ls to list files in case-insensitive order

bashcommand lineemacsunixzsh

How do I get the ls shell command to list my files in:

  1. case-insensitive order
  2. with the directories at the top?

The reason I ask is: I want Aquamacs Emacs dired to list my files and directories this way. Emacs queries ls when displaying dired.

Which means I don't think piping using a command like
ls -whatever | sort -f
will work. (Or will it? Can I do something fancy with aliases?)

I'm using zsh at the moment, but I'm not sure that matters for Emacs.

Similar questions have been asked here before and in other forums, but never resolved.

So, is this possible?

Best Answer

Emacs mode hooks to the rescue! Taken from EmacsWiki:

(defun mydired-sort ()
  "Sort dired listings with directories first."
  (save-excursion
    (let (buffer-read-only)
      (forward-line 2) ;; beyond dir. header 
      (sort-regexp-fields t "^.*$" "[ ]*." (point) (point-max)))
    (set-buffer-modified-p nil)))

(defadvice dired-readin
  (after dired-after-updating-hook first () activate)
  "Sort dired listings with directories first before adding marks."
  (mydired-sort))