Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

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."
share|improve this answer
Amazing, that worked! Anyone have any thoughts on case-insensitivity? – incandescentman Mar 7 '13 at 19:13
The above works, but it appears to have broken dired-details, which causes dired to display files this way: instead of this way:-rw-r--r--@ 1 peter peter 4000 Oct 5 18:21 Is there a way to make them play nicely together? – incandescentman Mar 7 '13 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. – patrix Mar 7 '13 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. – incandescentman Mar 7 '13 at 19:58
I'll give it a shot on the weekend. – patrix Mar 8 '13 at 6:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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