1

I am trying to use counsel-find-file to open the folder containing my Emacs configuration through a call such as the following:

(counsel-find-file 'nil "~/Documents/test-emacs/.emacs.d/")

This seems to work fine for the most part except when I am in a dired buffer. When I invoke the command from the dired buffer, counsel-find-file always opens the minibuffer into the directory that dired is in, not ~/Documents/test-emacs/.emacs.d.

I have attempted to invoke this command through both a general.el-configured key binding, and through M-:. Both methods produce the same results. Can someone explain why this happens and how to obtain the desired behavior of directing the minibuffer to ~/Documents/test-emacs/.emacs.d?

2
  • There's never any reason to quote nil. It evaluates to itself.
    – Drew
    May 8, 2022 at 22:48
  • Thanks for the heads up. May 9, 2022 at 4:20

1 Answer 1

1

I don't use Counsel. But from a quick look at its code:

It looks like counsel-find-file binds default-directory to use the INITIAL-DIRECTORY argument, but if the current buffer is in Dired mode then that binding is overridden to use the value of dired-current-directory instead.

And that function returns the value of the directory for the listing you are currently in in Dired. (E.g., if you are in a subdir listing then that subdir is used, else the top-level dir is used.)

(defun counsel--find-file-1 (prompt initial-input action caller)
  (let ((default-directory
         (if (eq major-mode 'dired-mode) ; <================
             (dired-current-directory)   ; <================
           default-directory)))
    ...))

[FWIW, I suspect the author of Counsel might want to change that (eq major-mode 'dired-mode) to use derived-mode instead. But that's unrelated to your question/problem.]

So the directory used in counsel--find-file-1 is the value of dired-current-directory from your current position in Dired.

This binding of default-directory in counsel-find-file is overridden by the binding in counsel--find-file-1:

(let ((tramp-archive-enabled nil)
      (default-directory (or initial-directory default-directory)))
  ...)

You'll notice, by the way, that the doc of counsel-find-file doesn't even mention argument INITIAL-DIRECTORY:

Forward to find-file.

When INITIAL-INPUT is non-nil, use it in the minibuffer during completion.

If it did, it would probably have to tell you something about its use, including the fact that it's apparently useless when in Dired.

Dunno whether this is a misfeature, a bug, or a feature. If you don't get a good answer here from someone else, and you think it perhaps shouldn't behave this way, consider reporting it to the Counsel maintainer.

1
  • Thanks for tracing this. I was able to get the desired behavior by rewriting that function to take the dired portion out. Still fairly new to Emacs hacking, so I don't know if this practice is frowned upon, or if this will break anything else...suppose I'll try it out for a while. May 9, 2022 at 4:28

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.