1

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
2

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

(defun my--choose-directory (directory-to-start-in)
  "Return a directory chosen by the user.  The user will be prompted
to choose a directory starting with `directory-to-start-in'"
  (let* ((ivy-read-prompt "Choose directory: ")
         (counsel--find-file-predicate #'file-directory-p)
         (default-directory directory-to-start-in)
         (selected-directory
          (ivy-read
           ivy-read-prompt
           #'read-file-name-internal
           :matcher #'counsel--find-file-matcher)))
    selected-directory))
2

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.