自定义Dired

15

我刚在维基百科上看到了这个dired模式屏幕截图。 我正在研究这些自定义设置。

关于颜色,我想只需指定正确的面孔即可,但是如何使dired默认显示文件大小为kbytes?以及MBs中的可用空间(顶部行)?


使用dired+或diredfl来设置颜色。 - Jim Balter
5个回答

19

我遇到了同样的问题,并找到了如何在运行时更改开关的方法。

因此,在dired缓冲区中

C-u s

您现在可以更改ls使用的开关。添加h可获取人类可读的文件大小

您还可以添加其他开关,例如我将其更改为-alsh,它现在按文件大小排序


与此键绑定相关的命令/函数是什么?describe-key在C-u处停止,因此它返回universal-argument - nephewtom
1
@nephewtom,这个是dired-sort-toggle-or-edit命令。在dired缓冲区中按下C-x k,然后再按s。这样您确实会将通用参数u发送到dired-sort-toggle-or-edit命令中。 - Aurélien Bottazini
1
请注意,为了按文件大小排序,'-alsh' 应更改为 '-alSh',大小写很重要。 - bph

14
为了以kbytes为单位获取文件大小,您可以自定义变量 dired-listing-switches 并使用 -k 选项:
(setq dired-listing-switches "-alk")

要获取MB中的总数/可用数量需要多做一些工作。这个方法在我的Linux系统上可行,但在我的Windows系统上获取可用数量失败了:

(setq directory-free-space-args "-Pm")
(defadvice insert-directory (after insert-directory-adjust-total-by-1024 activate)
  "modify the total number by dividing it by 1024"
  (save-excursion
(save-match-data
  (goto-char (point-min))
  (when (re-search-forward "^ *total used in directory \\([0-9]+\\) ")
    (replace-match (number-to-string (/ (string-to-number (match-string 1)) 1024)) nil nil nil 1)))))

谢谢回答。可用部分在 oxs 上也失败了(稍后我会再看一下)。 - Arthur Debert
1
@huaiyuan h选项会生成易读的输出,而不是使用固定大小(如请求所示)。总的来说,我还是喜欢h选项的。 - Trey Jackson
好的,抱歉打扰了;我看了一眼图片,它展示了人类可读的格式,所以我认为楼主想要那个。 - huaiyuan
OP可能想要这个。知道有一个人类可读的选项是很好的。 - Aurélien Bottazini

8
实际上,该截图几乎可以确定是来自于 Dired+,但其伴随的文本给人的印象是它来自于原版 Emacs (XEmacs?),且截图的归属为“Emacs 开发团队”。
因此,答案是你可以很容易地通过使用 Dired+ 并简单地自定义默认外观来获得该外观:M-x customize-group Dired-Plus

4

虽然您没有问,但我想补充一下...

我希望能够轻松地按大小和扩展名、名称和时间对dired输出进行排序。我知道可以使用M-x universal-argument dired-sort-toggle-or-edit来实现这一点,但是我希望将其放在s键上以便快速使用。

;; Redefine the sorting in dired to flip between sorting on name, size,
;; time, and extension,  rather than simply on name and time.

(defun dired-sort-toggle ()
  ;; Toggle between sort by date/name.  Reverts the buffer.
  (setq dired-actual-switches
        (let (case-fold-search)

          (cond

           ((string-match " " dired-actual-switches) ;; contains a space
            ;; New toggle scheme: add/remove a trailing " -t" " -S",
            ;; or " -U"

            (cond

             ((string-match " -t\\'" dired-actual-switches)
              (concat
               (substring dired-actual-switches 0 (match-beginning 0))
               " -X"))

             ((string-match " -X\\'" dired-actual-switches)
              (concat
               (substring dired-actual-switches 0 (match-beginning 0))
               " -S"))

             ((string-match " -S\\'" dired-actual-switches)
              (substring dired-actual-switches 0 (match-beginning 0)))

             (t
              (concat dired-actual-switches " -t"))))

           (t
            ;; old toggle scheme: look for a sorting switch, one of [tUXS]
            ;; and switch between them. Assume there is only ONE present.
            (let* ((old-sorting-switch
                    (if (string-match (concat "[t" dired-ls-sorting-switches "]")
                                      dired-actual-switches)
                        (substring dired-actual-switches (match-beginning 0)
                                   (match-end 0))
                      ""))

                       (new-sorting-switch
                        (cond
                         ((string= old-sorting-switch "t")
                          "X")
                         ((string= old-sorting-switch "X")
                          "S")
                         ((string= old-sorting-switch "S")
                          "")
                         (t
                          "t"))))
                  (concat
                   "-l"
                   ;; strip -l and any sorting switches
                   (dired-replace-in-string (concat "[-lt"
                                                    dired-ls-sorting-switches "]")
                                            ""
                                            dired-actual-switches)
                   new-sorting-switch))))))

  (dired-sort-set-modeline)
  (revert-buffer))

1
此外,dired 中的显示仅允许 9 个空格,因此对于非常大的文件,dired 中的显示将会混乱。这又需要重新定义一个函数。在这种情况下,需要从 ls-lisp.el 中重新定义一个函数:
;; redefine this function, to fix the formatting of file sizes in dired mode
(defun ls-lisp-format-file-size (file-size human-readable)
  (if (or (not human-readable)
          (< file-size 1024))
      (format (if (floatp file-size) " %11.0f" " %11d") file-size)
    (do ((file-size (/ file-size 1024.0) (/ file-size 1024.0))
         ;; kilo, mega, giga, tera, peta, exa
         (post-fixes (list "k" "M" "G" "T" "P" "E") (cdr post-fixes)))
        ((< file-size 1024) (format " %10.0f%s"  file-size (car post-fixes))))))

它只是将格式字符串中的9.0替换为11.0,将8.0替换为10.0。


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