/
DiredSortDirectoriesFirst
51 lines (39 loc) · 1.85 KB
/
DiredSortDirectoriesFirst
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
It seems pretty hard to achieve this simple task in Linux as the gnu-ls-command don't know any option to sort in this way. That lisp-code from the usenet looks hacky, but who cares - it works!
<pre>
(defun sof/dired-sort ()
"Dired sort hook to list directories first."
(save-excursion
(let (buffer-read-only)
(forward-line 2) ;; beyond dir. header
(sort-regexp-fields t "^.*$" "[ ]*." (point) (point-max))))
(and (featurep 'xemacs)
(fboundp 'dired-insert-set-properties)
(dired-insert-set-properties (point-min) (point-max)))
(set-buffer-modified-p nil))
(add-hook 'dired-after-readin-hook 'sof/dired-sort)
</pre>
Here is a better method that is based on the above code, but doesn't break after adding marks (and pressing 'g').
<pre>
(defun mydired-sort ()
"Sort dired listings with directories first."
(save-excursion
(let (buffer-read-only)
(forward-line 2) ;; beyond dir. header
(sort-regexp-fields t "^.*$" "[ ]*." (point) (point-max)))
(set-buffer-modified-p nil)))
(defadvice dired-readin
(after dired-after-updating-hook first () activate)
"Sort dired listings with directories first before adding marks."
(mydired-sort))
</pre>
[new:DrewAdams:2004-12-24 22:18 UTC]
See DiredSortMenu for info on FrancisWright's library <code>dired-sort-menu.el</code>. It lets you do this and much, much more. -- DrewAdams
----
I don't know if this is a new feature of GNU ls, but you can use the ``--group-directories-first'' option to accomplish this. Here's what I have in my init.el:
<pre>
(setq dired-listing-switches "-lXGh --group-directories-first")
</pre>
Alternatively, type C-u s while in a Dired buffer will prompt you for new ``ls'' switches which will let you sort the buffer any way you want that is available to ``ls''. The key is to read the ``ls'' manual :)
-- parolang
----
CategoryDirectories DiredMode