在OS X上dired排序出现错误

22

在OS X 10.5上,Emacs 23.2中的dired-mode,在使用前缀参数 --sort=extension-X 进行排序时,会出现以下错误:

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

然后dired缓冲区就变成了空的。我尝试设置

(setq dired-use-ls-dired nil)

但这没有任何效果。dired-sort-toggle-or-edit和按文件扩展名排序在我的Ubuntu系统上似乎工作正常。有人知道发生了什么吗?


1
非常感谢您创建了这样一个有用的问题,它引发了答案和评论,最终导致了解决方案。 - lawlist
嗯。我通过搜索emacs启动时出现的错误ls does not support --dired; see dired-use-ls-dired' for more details`找到了这个问题。所以,我看到/同意符号链接不是一个好主意。我已经安装了coreutils(感谢Homebrew,如果我没记错的话)(包括gls)。因此,ls-lisp解决方案很有趣,我认为@Marius列出的命令_单独_也应该可以工作。我很好奇@hatmatrix的解决方案是否有优势。试一下... - WHO's NoToOldRx4CovidIsMurder
是的,Marius的代码...让我们看看我是否喜欢这些开关...(是的,我打算很快清理这些注释。) - WHO's NoToOldRx4CovidIsMurder
我不这样做。建议人们将B添加到dired-listing-switches的值似乎对我来说是一个可怕的想法 - 如果你不先告诉他们它的作用!而且由于dired已经将这些文件着色为灰色,隐藏它们似乎不是一个好主意。我选择了(setq dired-listing-switches "-al --human-readable --group-directories-first"); - WHO's NoToOldRx4CovidIsMurder
由于gls运行良好,我将跳过ls-lisp - 因为我猜测gls的代码更好,而ls-lisp是为处理没有ls的情况而构建的。(如果有人想让我将这3个评论整理成1个答案,请@我。) - WHO's NoToOldRx4CovidIsMurder
5个回答

18

目前,我还发现了另一种解决方案,使用ls-lisp

(when (eq system-type 'darwin)
  (require 'ls-lisp)
  (setq ls-lisp-use-insert-directory-program nil))

你也可以使用eshell,它内置的ls命令理解-X。 - Eric Warmenhoven
2
可以将 insert-directory-program 设置为 eshell 的 ls 吗? - hatmatrix
感谢@crippledlambda的帮助,我结合了这个答案和duma在之前评论中的建议,将我的insert-directory-program设置为gls,在我的mac OSX上一切都正常工作了。 - mcheema
这个让我省了很多麻烦。实际上我在使用cygwin emacs的windows PowerShell,但是遇到了完全相同的问题。ls不接受任何“长”参数。谢谢!!! - Matt Messersmith

10

在OS X上安装的ls命令不支持-X或任何类似--sort的长参数。设置dired-use-ls-dired也没有效果;dired将始终使用ls,但如果该变量为非空,则会将--dired传递给ls。

如果您想使用那种类型的排序,可以尝试使用fink安装coreutils,这将提供更像Ubuntu中您习惯的ls。


啊,原来是这个问题…… FreeBSD 没有那种类型的排序方式很奇怪。我以为 Fink 已经死了,但实际上看起来还在活跃……不知道为什么我会这么想。谢谢。 - hatmatrix
7
我通过Homebrew安装了coreutils,它似乎安装了/usr/local/bin/gls。我使用ln -s命令将其链接到/usr/local/bin/ls,然后我再也没有在Emacs中看到错误了。 - duma
6
在Yosemite 10.10.3下,这对我无效。然后我另外使用了(setq insert-directory-program "/usr/local/bin/gls"); 使用适当的GNU ls放在.emacs中,这样就有效了。 - Marius Hofert

9
以下是在Snow Leopard 10.6.8上使用通过macports安装的coreutils运行Emacs的步骤:
注意:我的macports安装不同于通用的(/opt/...)--即,我使用/macports作为根目录。更改根设置并非必需,这只是我的个人偏好。对于原始的macport安装或其他设置,请相应调整路径。
sudo /macports/bin/port install coreutils

这段代码需要放在.emacsinit.el文件中:

;; sort directories first

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

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

注意:不建议为gls/ls使用符号链接,因为它会破坏macports安装的功能,可能还会影响其他东西。


对于希望获得更多控制权的用户,可以尝试以下替代安装方法:

下载:coreutils-8.21.tar.xz,下载链接:http://ftp.gnu.org/gnu/coreutils/

如果您没有解压缩*.xz文件的工具,可以使用TheUnarchiver3.9.1等工具。

以下是快速参考指南,可将coreutils安装在自定义位置,而非默认位置:

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

make

sudo make install

将以下内容插入到你的 .emacsinit.el 文件中 -- 根据实际情况调整路径:

;; sort directories first

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

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

7

2020年了还在发生!如果你和我一样,使用 brew 作为你的开源软件包管理器,并且已经通过 brew install coreutils 安装了 coreutils,那么这是正确的解决方案,可以复制粘贴到你的 .emacs 文件或任何你保存启动自定义的地方:

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

我检查操作系统,因为我在多个系统上部署我的Emacs配置。

当前路径:

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

之前(2022年之前?)它是这样的:

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

通常情况下,如果您已将gnu ls配置为首选的默认shell,则始终可以通过键入which ls来检查其当前完整路径。

奇怪的是,在我长期使用Emacs的Mojave系统上,这突然发生了,并且我确定dired在过去是可以正常工作的。我猜测更新破坏了某些东西,使得dired可以使用正确的二进制文件,而无需手动设置。


2
对于任何遵循此答案的人,您还需要安装coreutils(通过brew)才能使其正常工作。这可以使用命令brew install coreutils来完成。 - D. Gillis
1
Homebrew更改了默认路径。现在Coreutils ls的路径为/opt/homebrew/opt/coreutils/libexec/gnubin/ls - Sven Koschnicke

0
这与lawlist的好答案并没有太大区别,但提供了稍微不同的信息,并针对那些使用Nix软件包管理器的人进行了定制。
(use-package dired
  :custom
  ;; See https://dev59.com/GG855IYBdhLWcg3w1oLa
  ;; 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://dev59.com/xG855IYBdhLWcg3w6Y5q][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"))))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接