2

I use C-x d *.org to list only files with .org extension. Is there a way to make this permanent for that directory? I have to do this every time I open that directory, it would be nice to have it permanently.

1 Answer 1

2

EDIT: add better solution in response to comment

The earlier given solutions were the easiest I could think of (and I hoped they would satisfy your requirements). However, I now found that the exact solution to your question actually is more or less as easy. This time, we just add the following :filter-args advice on dired-noselect:

(defun my-dired-org-filter-advice (args)
  (let ((dir (expand-file-name (file-name-as-directory (car args)))))
    (when (string= dir "/full-path-to-dir/") ; include final slash here
      (setf (car args) (concat dir "*.org")))
    args))

(advice-add 'dired-noselect :filter-args #'my-dired-org-filter-advice)

I'll keep the original answer below for the alternative solutions and for the 'modification' suggestions.

END EDIT


It is probably easiest to simply define a custom command for it. The exact form of the function then depends on your exact requirements. Here is a simplest example, in case you'd only use it for opening a single directory.

(defun my-open-org-dir ()
  (interactive)
  (dired "/path-to-dir/*.org"))

Of course, the function can be made more general, where you could prompt for selecting a dir first.

After evaluating the above code, e.g. by adding it to your init file, and restart Emacs, you can call the function via M-x or bind the function to some key(-sequence).

If you prefer to 'modify' the original dired (C-x d) command, then you could add a :filter-return advice on the function dired-read-dir-and-switches to conditionally add (concat) *.org after the filename for certain (matching) directories. For example, you could use the following advice for it:

(defun my-dired-org-filter-advice (output)
  (let ((dir (car output)))
    (when (string= dir "/full-path-to-directory/") ;; include the final slash
      (setf (car output) (concat dir "*.org")))
    (print output)))

(advice-add 'dired-read-dir-and-switches :filter-return #'my-dired-org-filter-advice)

where, of course, you should fill in the correct full directory path. If you'd like to use it for multiple dirs, then replace string= by member and pass your list of directories.

12
  • Thanks, but I didn't exactly understand how I can use this. I will be pasting the code to my .emacs then how do activate it?
    – zeynel
    Dec 17, 2022 at 15:48
  • OP: You invoke it using M-x my-open-org-dir. Or bind that command to a key and use the key to invoke it.
    – Drew
    Dec 17, 2022 at 16:53
  • @dalanicolai: You might want to show an example of defining the advice.
    – Drew
    Dec 17, 2022 at 16:55
  • 1
    I have added usage instructions and an example advice to the answer. Dec 17, 2022 at 19:52
  • 1
    I always prefer solutions where the code is generic and I can influence it from the outside, without having to modify it. But you are right: that's a different question.
    – NickD
    Dec 19, 2022 at 16:22

Your Answer

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

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