tags:

views:

166

answers:

3
+4  Q: 

Cutomizing dired

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

Regarding colors, I guess 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)?

+4  A: 

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)))))
Trey Jackson
On Linux, try "-alh".
huaiyuan
Tks for the answer. The available portion fails on oxs too (I'll take a look into it later on).
Arthur Debert
@huaiyuan The `h` option does human readable, it doesn't use a constant size (as was the request). I do like `h` in general tho.
Trey Jackson
Ok, sorry for the noise; I took a glance at the picture, which shows the human readable format, and thought the OP wanted that.
huaiyuan
+1  A: 

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))
Cheeso
+1  A: 

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)

Cheeso