2

I have recently started using ivy, and I've been very happy with it. The only behaviour which I've found quite surprising, is the combination of completions and read-file-name. As an example, if I evaluate the following snippet with ivy switched off, and just press return, I get the expected result that "test" is printed.

(let* ((default "test")
       (name (read-file-name (format "Master file (default %s): "
                                     (or default "this file"))
                             nil default)))
  (print name))

If instead I evaluate the code with ivy running, and just press return, ivy selects the first filename completion candidate (./), and /home/myusername is printed. There are quite a few different packages that I use which ask for filenames, but supply a sensible default value.

Ideally, I would like to be able to press return to use the default value, or start typing and have all of ivy's completion facilities handy. Is there any way of achieving that?

1 Answer 1

3

read-file-name has lots of optional parameters. You are supplying default as the value for default-filename. The documentation says that this is the value used if the user exits with the same non-empty string inserted by this function.

What you need to do is provide this non-empty string, which is the value of the parameter initial. So you need

(let* ((default "test")
       (name (read-file-name (format "Master file (default %s): "
                                     (or default "this file"))
                             nil default nil (or default "this file"))))
  (print name))

as ivy is providing an initial string of the current directory if you don't provide one.

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.