I would like to write a function that would prompt the user to select a directory, using an UI like that presented by counsel-find-file.

The starting directory should be an arbitrary directory passed to this function as an argument.

This function should only show the user directories, not files, and the user should be able to navigate up and down the filesystem just as in counsel-find-file.

When the user makes their selection by hitting the RETURN key, the path to the selected directory should be returned to the calling function.


1 Answer 1


I just figured this out myself (using counsel-dired as a model):

(defun my-counsel-find-directory (&optional start-dir)
  "Return a directory chosen by the user.
The user is prompted to choose a directory starting with START-DIR."
  (let ((ivy-read-prompt "Choose directory: ")
        (counsel--find-file-predicate #'file-directory-p)
        (default-directory (or start-dir default-directory)))
     :matcher #'counsel--find-file-matcher)))

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.