If I execute a M-x find-name-dired command the output is a *Find* buffer containing the results.

However if I perform a subsequent find-name-dired the contents of the *Find* buffer are overwritten with the latest output.

Is there a means to execute multiple find-name-dired commands and keep the output buffer of each?

In other words, have multiple *Find* buffers?

  • 1
    Please don't post the same question to both StackOverflow and emacs.SE - choose one. Please delete one of the posts. Thx. – Drew Nov 15 '19 at 18:08

Looking at the code, find-name-dired delegates to find-dired.

Looking at the find-dired code, it opens a buffer with name "*Find*". From its source:

(get-buffer-create "*Find*")

So if we rename the first buffer we made, the second call to find-name-dired will not touch it. You can rename the buffer with M-x rename-buffer (lets you choose a name) or M-x rename-uniquely (adds a numerical suffix <2>, <3>, …).

Rather than renaming by hand, here's a wrapper function around find-name-dired that changes the buffer name after it's ran. Note that it will reuse any preexisting buffer called *Find*, so prior searches ran with find-name-dired or other find commands will go away.

(defun find-name-dired-with-unique-buffer-name (dir pattern)
  "Search DIR recursively for files matching the globbing pattern PATTERN,
and run Dired on those files.
PATTERN is a shell wildcard (not an Emacs regexp) and need not be quoted.
The default command run (after changing into DIR) is

    find . -name \\='PATTERN\\=' -ls

See `find-name-arg' to customize the arguments."
     "DFind-name (directory): \nsFind-name (filename wildcard): ")
    (find-name-dired dir pattern)
    (let ((buffer-name (format "*Find: %s in %s*" pattern dir)))
      "If we previously searched for the same thing, kill that buffer for reuse"
      (when (get-buffer buffer-name)
        (kill-buffer buffer-name))

      ;;we are left in a buffer called "*Find*"
      (rename-buffer buffer-name)))
  • 1
    Thanks! The only weakness of this solution is that if we search for the same pattern in two directories, the first is killed. That can be worked-around by also adding the directory to the buffer name. – Realraptor Nov 15 '19 at 16:00
  • 1
    @Realraptor sure, why not? Added. – zck Nov 15 '19 at 17:36

If you use library find-dired+.el then the buffer used is not *Find*. Instead, it has same name (using directory-file-name) as argument DIR (the directory).

So if you change directory then you get a different buffer.

If that's not enough then you can always use hook find-dired-hook to rename the buffer each time (or whatever).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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