I'm looking for something similar to (find-buffer-visiting FILENAME &optional PREDICATE) that would find a dired-mode buffer visiting a specified directory. Something like:

(find-buffer-visiting-dir DIRNAME)

The built-in find-buffer-visiting only searches buffer visiting files quite unfortunately. It checks if the buffer has a local buffer-file-name variable bound. The optional PREDICATE is therefore used only on files.

A potential improvement for that function would be to write (find-buffer-visiting FILENAME &optional PREDICATE FILTER) where FILTER would be a filter function used on all buffers to determine if the buffer should be included in the search.

Is there something like this already available?

  • 1
    If you are using dired+ you can check diredp--reuse-dir-buffer-helper. Several interactive commands in dired+ use this helper. But, I am not sure whether it really does what you want. (Your question smells a bit like an XY-problem.)
    – Tobias
    Jan 7, 2023 at 19:42
  • I wrote a command that opens a file at point and which simply moves point to a window already opening that file. I wanted it to work for directories as well and in my code I'm using find-buffer-visiting on the filename to get the buffer. So I was looking to expand on that call either by replacing it with a function being able to detect the buffer of a directory or adding a or form which would then call the second function identifying the buffer of a directory. I don't think it was an XY-problem. The solution is to use the dired-buffers-for-dir as Drew pointed out.
    – PRouleau
    Jan 7, 2023 at 21:47

1 Answer 1


Sounds like you're looking for function dired-buffers-for-dir.

Dired+ enhances this function, but even the vanilla Emacs version is OK. C-h f dired-buffers-for-dir tells you (this is the Dired+ doc string):

dired-buffers-for-dir is a compiled Lisp function in dired+.el.

(dired-buffers-for-dir DIR &optional FILE SUBDIRS-P)

Return a list of buffers that Dired DIR (top level or in-situ subdir).

DIR is automatically expanded with expand-file-name using the default-directory. If you need expansion relative to some other directory then do that before calling dired-buffers-for-dir.

If FILE is non-nil, include only those whose wildcard pattern (if any) matches FILE.

If SUBDIRS-P is non-nil, also include the Dired buffers of directories below DIR.

The list is in reverse order of buffer creation, most recent last. As a side effect, killed Dired buffers for DIR are removed from dired-buffers.

In general, if it seems like there should be a utility function for dealing with files or directories, or their names etc., then it's likely that there is in fact such a function. Remember that the apropos commands are your friends. In this case, just M-x apropos dired buffer shows you this function (and others).

  • I'll definitively have to use apropos more often.
    – PRouleau
    Jan 7, 2023 at 21:48
  • There are additional apropos commands and non-interactive functions, in particular apropos-documentation.
    – Drew
    Jan 7, 2023 at 22:57
  • I know, they're all very useful. apropos-value is quite useful too. I have just not developed the habit to use them enough.
    – PRouleau
    Jan 7, 2023 at 23:11

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.