2

I add (setq dired-listing-switches "-lXGh --group-directories-first") in my init.el

But when I go into dired mode.

ls does not support --dired; see ‘dired-use-ls-dired’ for more details.
insert-directory: Listing directory failed but ‘access-file’ worked

this error occurs. How can I solve this problem? I want to sort directories first in dired mode. My environment is macOS Sierra and emacs 25.1

  • On OSX, I still use Core Utilities to have a version of ls that permits me to sort directories first. I haven't yet upgraded to Sierra, so I don't know what type of ls version it has: See: "error in dired sorting on OS X": stackoverflow.com/a/20027876/2112489 In a nutshell, certain versions of ls do not support group directories first. You can open a shell and type help for ls to see what command line options are supported. – lawlist Dec 4 '16 at 5:16
4
  1. Use standard Emacs library ls-lisp.

    (require 'ls-lisp)
    
  2. Customize these two user options (you can just use setq to set them in your init file, if you prefer):

    • ls-lisp-dirs-first to t
    • ls-lisp-use-insert-directory-program to nil

If you want to be able to sort Dired listings in many ways, using a menu, then load library dired-sort-menu.el. (You might also want to use dired-sort-menu+.el.)

3

As @lawlist mentioned, you need to install coreutils as the ls command which ships with mac does not have the option to sort directories first. If you have homebrew, you could just type brew install coreutils. This will also install gls which emacs can use. Add this to your .emacs :-

(setq insert-directory-program "gls" dired-use-ls-dired t)

After adding this, your dired listing values will work again on macOS

2

To get rid of the error, you could set

(setq dired-use-ls-dired  nil)

(see entering dired in the manual)


I don't know if mac os x's ls now supports sorting in the way you intended, though. I installed coreutils via homebrew to have a GNU ls. And set

(setq insert-directory-program "/usr/local/bin/ls")
;; (setq dired-listing-switches "your settings")

If the ls-use-ls-dired option above does not work for you, and you don't want to install a separate binary, you could use emacs' ls emulation via

(setq ls-lisp-use-insert-directory-program nil)
(require 'ls-lisp)

and then set

(setq ls-lisp-dirs-first t)

see ls in lisp in the manual.


nb: Best wrap the options of your choice into a

(add-hook 'dired-load-hook
  (lambda () ...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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