0

I use the following initial code to open one om my frequently used folders:

(global-set-key (kbd "C-6") (lambda() 
    (interactive)
    (find-file (format "C:/Users/%s/notes" user-login-name) )))

If I left point at line 16 last time I killed the dired buffer for the folder, dired will open my folder with point at line 16.

How can I always open this folder with point at line 1 (..) ?

How can I alway open this folder with reversed name sorting ?

EDIT (2019-07-17--08-55-47): my questions were not complete. I need this:

1) keep the two links "." and ".." on the first and second line, as normal

2) then place all my folders below the two first lines

3) then sort all files below the folders, sorted in reverse order

Adding an "r" to (setq dired-listing-switches "-alr") sorts 1) and 2) to the very bottom, not very practical for me.

1
  • To get reverse name sorting, use SWITCHES arg to dired, specifying switch -r. M-x man ls is your friend.
    – Drew
    Jul 17, 2019 at 0:30

1 Answer 1

1

I do not see the "point at line 16" behavior you see: every time I open a directory after killing the buffer, the cursor is on the first file (third line in the buffer, with the default -al switches). Are you sure you are killing the buffer?

For the reverse sorting, as @Drew points out, calling dired with a prefix argument (C-u M-x dired) allows you to specify the SWITCHES argument: you can add an r to the default -al and get a reversed listing. If you want to have reverse listings always, add this line to your init file:

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

or customize the dired-listing-switches variable.

If you insist on getting to dired through find-file, things get a bit harder: I would let-bind the above variable in your function:

(global-set-key (kbd "C-6") (lambda() 
    (interactive)
    (let ((dired-listing-switches "-alr"))
        (find-file (format "C:/Users/%s/notes" user-login-name)))))

If you want to go to the beginning of the buffer every time you press C-6 (whether the buffer exists already or not), you can add a (goto-char (point-min)) to your function:

(global-set-key (kbd "C-6") (lambda() 
    (interactive)
    (let ((dired-listing-switches "-alr"))
        (find-file (format "C:/Users/%s/notes" user-login-name))
        (goto-char (point-min)))))

EDIT: re.the revised sorting spec - I don't think that's possible with ls switches. You can try adding --group-directories-first to the dired-listing-switchtes, but the -r will reverse the order of the directories as well, so all the directories will appear first in reverse order (so . and .. will be at the end of the list of directories) and then all the files will appear in reverse order. I'm not even sure that --group-directories-first is available on Windows.

4
  • Thanks! It seems you'r right. "The point at line 16" behavior did not turn up again.
    – myotis
    Jul 17, 2019 at 6:44
  • The -r switch have two side effect, given in my EDIT comment above, it makes both the ".", ".." and all folders to be sorted to the bottom. I need them in the top.
    – myotis
    Jul 17, 2019 at 6:59
  • Your added code line, that with the "let" function, seems to have a mismatch of "(" and ")" that is not corrected for further down in the code. Im I right?
    – myotis
    Jul 17, 2019 at 7:33
  • You are right: I was missing a closing paren at the end of each form. Fixed now. Thanks!
    – NickD
    Jul 17, 2019 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.