OS X 下 dired mode 中使用 SPC f E 打开当前目录失败的解决方法

如果在 OS X 上安装了 GNU coreutils,又启用了 osx Layer,那么很可能在 dired mode 中使用 SPC f E (sudo edit) 会失败,解决方法:

  • 1 代码:
(with-eval-after-load 'tramp-sh
  (defun tramp-get-ls-command (vec)
    (with-tramp-connection-property vec "ls"
      (tramp-message vec 5 "Finding a suitable `ls' command")
      (or
       (catch 'ls-found
         (dolist (cmd '("gnuls" "gls" "ls"))
           (let ((dl (tramp-get-remote-path vec))
                 result)
             (while (and dl (setq result (tramp-find-executable vec cmd dl t t)))
               ;; Check parameters.  On busybox, "ls" output coloring is
               ;; enabled by default sometimes.  So we try to disable it
               ;; when possible.  $LS_COLORING is not supported there.
               ;; Some "ls" versions are sensible wrt the order of
               ;; arguments, they fail when "-al" is after the
               ;; "--color=never" argument (for example on FreeBSD).
               (when (tramp-send-command-and-check
                      vec (format "%s -lnd /" result))
                 (when (tramp-send-command-and-check
                        vec (format
                             "%s --color=never -al /dev/null" result))
                   (setq result (concat result " --color=never")))
                 (throw 'ls-found result))
               (setq dl (cdr dl))))))
       (tramp-error vec 'file-error "Couldn't find a proper `ls' command")))))
  • 2 删除 ~/.emacs.d/.cache/tramp
  • 3 修改 osx/post-init-exec-path-from-shell @ ~/.emacs.d/layers/+os/osx/packages.el :
(defun osx/post-init-exec-path-from-shell ()
  ;; Use GNU ls as `gls' from `coreutils' if available.  Add `(setq
  ;; dired-use-ls-dired nil)' to your config to suppress the Dired warning when
  ;; not using GNU ls.  We must look for `gls' after `exec-path-from-shell' was
  ;; initialized to make sure that `gls' is in `exec-path'
  (when (spacemacs/system-is-mac)
    (let ((gls (executable-find "gls")))
      (when gls
        (setq insert-directory-program gls
              dired-listing-switches "--quoting-style=literal -aBhl --group-directories-first")))))