开发者

Customizing dired

开发者 https://www.devze.com 2022-12-30 12:31 出处:网络
I just came across this dired mode screen at Wikipedia. I am looking into those customizations. Regarding colors, I guess开发者_StackOverflow just specifying the correct faces will do, but how do I

I just came across this dired mode screen at Wikipedia. I am looking into those customizations.

Regarding colors, I guess开发者_StackOverflow just specifying the correct faces will do, but how do I get dired to show file sized in kbytes by default? And the available space in MBs (top line)?


I came up with the same problem and found how to change swithes on the fly.

So while in a dired buffer

C-u s

you can now change switches used by ls. Add h do get a human readable file sizes

You can add other switches too, for example I changed it to -alsh and it now sorts by file size


To get the file sizes in kbytes, you can customize the variable dired-listing-switches to use the -k option:

(setq dired-listing-switches "-alk")

You have to do a little more work to get the total/available numbers in MB. This worked on my Linux system, but the available portion failed to work on my Windows box:

(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)))))


Actually, that screenshot is almost certainly of Dired+, though the accompanying text gives the impression that it is from vanilla Emacs (XEmacs?), and the attribution for the screenshot is "Emacs development team".

So yes, the answer is that you can easily get that appearance, by using Dired+ and simply customizing the default faces: M-x customize-group Dired-Plus.


You didn't ask, but I thought I'd add....

I wanted to be able to easily sort the dired output by size and extension, as well as name and time. I know I can do this with M-x universal-argument dired-sort-toggle-or-edit, but I wanted it available on the s key to make it quick.

;; 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))


Also, the display in dired allows only 9 spaces, so for very large files, the display in dired will get messed up. Once again that required redefining a fn. In this case, one from 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))))))

(it just replaces the 9.0 with an 11.0, and the 8.0 with a 10.0, in the format strings)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号