2

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

1 Answer 1

3

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)))
    (ivy-read
     ivy-read-prompt
     #'read-file-name-internal
     :matcher #'counsel--find-file-matcher)))
2
  • How do I get this source code to be highlighted as elisp?
    – izkon
    Commented Oct 20, 2020 at 22:27
  • 1
    StackExchange/Overflow switched from Prettify to highlight.js recently, and Prettify supports elisp, but highlight.js does not, you can use clojure/common-lisp/scheme/etc instead if you want, see emacs.stackexchange.com/editing-help#syntax-highlighting
    – xuchunyang
    Commented Oct 20, 2020 at 23:02

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.