22

On OS X 10.5 Emacs 23.2, in dired-mode, if I try to sort by dired-sort-toggle-or-edit with prefix argument --sort=extension or -X, I get:

insert-directory: Listing directory failed but `access-file' worked

and the dired buffer becomes empty. I tried setting

(setq dired-use-ls-dired nil)

but this had no effect. dired-sort-toggle-or-edit and sorting by extension seems to work okay on my Ubuntu box. Anyone have a clue what's going on?

Drew
  • 29,895
  • 7
  • 74
  • 104
hatmatrix
  • 42,883
  • 45
  • 137
  • 231
  • 1
    Thank you very much for creating such a useful question, which spawned answers and comments that lead to the solution. – lawlist Nov 17 '13 at 06:29
  • Hmm. I found this question by searching for the error I started getting on emacs startup: `ls does not support --dired; see `dired-use-ls-dired' for more details`. So, I see/agree that a symlink isn't a great idea. I have coreutils (thanks to Homebrew, IIRC) (including gls) installed already. So, the ls-lisp solution is intriguing, and I think the command @Marius lists _alone_ should work too. I'm curious if @hatmatrix's solution has advantages. Trying 'em... – WHO'sNoToOldRx4Covid-CENSORED Aug 15 '18 at 18:48
  • Yup, Marius' works... let's see if I like the switches... (yes, I intend to clean these comments up soon.) – WHO'sNoToOldRx4Covid-CENSORED Aug 15 '18 at 18:58
  • I do not. Advising folks to add a B to the value of dired-listing-switches seems like a terrible idea to me - if you don't TELL THEM what it does first! And since dired colors such files grey already, hiding them seems like a not-great idea. I went with `(setq dired-listing-switches "-al --human-readable --group-directories-first");` – WHO'sNoToOldRx4Covid-CENSORED Aug 16 '18 at 01:11
  • Since gls works well, I'll skip ls-lisp - as I'm guessing the code of gls is better, and ls-lisp was built to handle situations where an ls wasn't available. (If anyone wants me to clean up these 3 comments into 1 answer, please ping me.) – WHO'sNoToOldRx4Covid-CENSORED Aug 16 '18 at 01:18

5 Answers5

18

For now, I've also found another solution using ls-lisp

(when (eq system-type 'darwin)
  (require 'ls-lisp)
  (setq ls-lisp-use-insert-directory-program nil))
hatmatrix
  • 42,883
  • 45
  • 137
  • 231
10

The ls that's installed on OS X doesn't support -X or any long arguments like --sort. Setting dired-use-ls-dired won't have any effect; dired will always use ls, but if that variable is non-nil, it will pass --dired to ls.

If you want that type of sorting, you can probably use something like fink to install coreutils, which will provide an ls more like what you're used to in Ubuntu.

Eric Warmenhoven
  • 2,942
  • 20
  • 17
  • Ah, so that's the problem... odd that FreeBSD wouldn't have that type of sorting. And I thought fink was dead but actually looks active... wonder why I'd thought that. Thanks. – hatmatrix Nov 03 '10 at 03:24
  • 7
    I installed `coreutils` through Homebrew, and it apparently installs `/usr/local/bin/gls`. I `ln -s`'d this to `/usr/local/bin/ls` and then I didn't get the error in Emacs any more. – duma Feb 19 '13 at 15:19
  • 6
    @duma: This did not work for me under Yosemite 10.10.3. I then used, additionally, `(setq insert-directory-program "/usr/local/bin/gls"); use proper GNU ls` in `.emacs` and it worked – Marius Hofert May 12 '15 at 17:27
9

Here are the steps for Emacs running on Snow Leopard 10.6.8 using coreutils installed through macports:

NOTE:   My macports installation is different than the generic (/opt/...) -- i.e., I use /macports as the root. Altering the root setup is not required, it is just a personal preference of mine. For vanilla macport installations or alternative setups, adjust the path accordingly.

sudo /macports/bin/port install coreutils

This goes inside the .emacs or init.el:

;; sort directories first

(setq insert-directory-program "/macports/bin/gls")

(setq dired-listing-switches "-aBhl --group-directories-first")

NOTE:   Using a symlink for gls/ls is not recommended because it breaks functionality with macports install and most likely other stuff too.


Alternative installation for users who want more control:

Download: coreutils-8.21.tar.xz from:  http://ftp.gnu.org/gnu/coreutils/

If you do not have a utility to unzip an *.xz file, you can use a utility such as TheUnarchiver3.9.1.

Here is a quick reference to make the coreutils -- I set the installation location to my own personal preference instead of the default:

./configure \
--prefix=/Users/HOME/.0.data/.0.emacs/elpa

make

sudo make install

Insert these into your .emacs or init.el file -- adjust the path accordingly:

;; sort directories first

(setq insert-directory-program "/Users/HOME/.0.data/.0.emacs/elpa/bin/ls")

(setq dired-listing-switches "-aBhl --group-directories-first")
lawlist
  • 13,099
  • 3
  • 49
  • 158
7

Still happening in 2020! If, like me, you're using brew as your open source package manager, and you have already installed coreutils with brew install coreutils, this is the right solution to copy paste in your .emacs file, or wherever you keep your startup customizations:

(when (equal system-type 'darwin)
  (setq insert-directory-program "/insert/here/path/to/homebrew/ls"))

(I check for the OS because I deploy my Emacs configuration on multiple systems).

Current path:

/opt/homebrew/opt/coreutils/libexec/gnubin/ls

Previously (before 2022?) it used to be:

/usr/local/opt/coreutils/libexec/gnubin/ls

As usual, if you have configured gnu ls as your default in your current shell of choice, you can always check its current full path by typing which ls.

Oddly enough, this happened suddenly on a Mojave system on which I routinely use Emacs since forever, and on which I am sure dired was working in the past. I guess that an update broke something that made dired use the correct binary, without having to manually set it.

ggrocca
  • 161
  • 2
  • 3
  • 2
    For anyone following this answer, you also need to install coreutils (through brew) for this to work. This can be done with the command `brew install coreutils`. – D. Gillis Nov 18 '21 at 17:03
  • 1
    homebrew changed it's default path. The path to coreutils ls is now `/opt/homebrew/opt/coreutils/libexec/gnubin/ls` – Sven Koschnicke Mar 21 '23 at 08:59
0

This is not much different than lawlist's nice answer, but has slightly different information and is tailored to those who use the Nix package manager:

(use-package dired
  :custom
  ;; See http://stackoverflow.com/questions/4115465/emacs-dired-too-much-information
  ;; NOTE: Just some information worth keeping in mind. More readable dired file
  ;; size output - consider adding F (make file type obvious), or p (p adds a
  ;; trailing slash to dirs, but makes moving dirs fail), and G (colorize) too.
  (dired-listing-switches "-alh --group-directories-first")
  :config
  ;; [[https://stackoverflow.com/questions/4076360/error-in-dired-sorting-on-os-x][macos - error in dired sorting on OS X - Stack Overflow]]
  ;; To fix the
  ;; (error "Listing directory failed but 'access-file' worked")
  ;; error. Emacs needs to use gnu's ls, which I get through nixpkgs' coreutils.
  ;; In my config, currently, Emacs is not picking up the path to my nix install
  ;; ls (todo: fix).
  ;;
  ;; Note that, unlike the info at the link provided above,
  ;; --group-directories-first is not needed to fix this error. I just like to
  ;; see the directories first in a dired buffer.
  (setq insert-directory-program (expand-file-name ".nix-profile/bin/ls"
                                                   (getenv "HOME"))))
Joe
  • 965
  • 11
  • 16