11

Windows 10, Emacs 25.1, Dired+

Here example of my folder's content:

dired

As you can see the files and folder are mixed. But I want first to show list of folders and after show list of files. Is it possible?


With respect to the answer by @Drew:

I install dired-sort-menu and add in my init.el the next line:

(add-hook 'dired-load-hook
          (lambda () (require 'dired-sort-menu)))

and restart Emacs. But ... how open sort menu? When I mouse right click on Dired buffer I get another menu.

Here my screenshot:

dired_right_mouse_click

2 Answers 2

18

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.

2
  • 1
    Googling also reveals some other (hack-ish) suggestions on the emacswiki.
    – aplaice
    Commented Oct 20, 2017 at 18:05
  • 1
    Yes! (setq ls-lisp-dirs-first t) - this help. So I not need do download dired-sort-menu.el. Thanks. Commented Oct 21, 2017 at 13:01
5

You can use Dired Sort Menu to sort Dired in various ways, including putting the directories first.

enter image description here


The answer to the question you asked after you loaded Dired Sort Menu and Dired+ is this: Use menu-bar menu Dir > Sort By.

If you have not turned on menu-bar-mode then you need to do that to see menu-bar menus (such as Dir).

(The screenshot you show for mouse-3 has the title This File. All of its actions apply to the file of the current line. If you select a region with the mouse then you get a different mouse-3 popup menu, with actions that act on all files in the region. Neither of these menus applies to all of Dired. Use the menu-bar menus for that.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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