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?

1 Answer 1


Emacs mode hooks to the rescue! Taken from EmacsWiki:

(defun mydired-sort ()
  "Sort dired listings with directories first."
    (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."
  • Amazing, that worked! Anyone have any thoughts on case-insensitivity? Commented Mar 7, 2013 at 19:13
  • The above works, but it appears to have broken dired-details, which causes dired to display files this way: filename.org instead of this way:-rw-r--r--@ 1 peter peter 4000 Oct 5 18:21 filename.org. Is there a way to make them play nicely together? Commented Mar 7, 2013 at 19:24
  • I don't have dired-details here, you may need to adjust the second regexp in sort-regexp-fields. For case-insensitive sorting, setting sort-fold-case to non-nil should do the trick.
    – nohillside
    Commented Mar 7, 2013 at 19:34
  • I'm a newbie. Can you tell me how to set sort-fold-case (I don't find it as a variable when I do C-h v). And can you suggest syntax for how I might adjust the second regexp in sort-regexp-fields so I can play with it? Thank you. Commented Mar 7, 2013 at 19:58
  • I'll give it a shot on the weekend.
    – nohillside
    Commented Mar 8, 2013 at 6:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .