External ls
GNU's ls
has an option --group-directories-first
, and you can control the list of options passed to ls
by dired with the variable dired-listing-switches
(assuming that you haven't already customized this, you'd set it to -al --group-directories-first
).
Unfortunately, on Windows, emacs usually uses lisp emulation of ls
and as you can see from the docstring of ls-lisp--insert-directory
, not all switches are supported — in particular, --group-directories-first
is not.
However, if you use cygwin (or possibly WSL (?)) and have ls
installed, then you can tell emacs to use the external ls
program instead of lisp emulation:
(setq ls-lisp-use-insert-directory-program t) ;; use external ls
(setq insert-directory-program "c:/cygwin/bin/ls") ;; ls program name
(See the info manual.)
Emulated lisp ls
Based on Drew's great answer, looking at the source of dired-sort-menu.el
, it appears that there's a (built-in) setting to sort directories before other files in the emulated lisp ls:
(setq ls-lisp-dirs-first t)
(This will only have an effect if you have ls-lisp-use-insert-directory-program
set to nil
.)
I'm listing this in addition to Drew's answer, in case you might not want to download/use dired-sort-menu.el
, since it's not actually necessary — it just provides a very nice, convenient interface.