I have been trying to implement a function with ivy that needs to run a particular function not only on selection but also on every selection change in the list.

To be specific, when the user scrolls the list of available options, the currently selected item from the list should be identifiable.

(ivy-read "Choose: "
          (list "one" "two" "three")
          :action (lambda (n)
                    (do-something n))
          :update-fn #'do-something-else)

The above code runs the lambda when a particular item (either one, two, or three) is selected and the return key is pressed. Also, with the update-fn in place, even typing one of the values does do-something-else and I can read the value typed as ivy-text. However, I need some action to be triggered when the user scrolls through the list of numbers without even typing.

Is that even possible?

1 Answer 1


After a lot of research, I learned that there is no direct way of doing this through ivy-read.

There is however a way that one can use C-M-p and C-M-n to be able to navigate through the list and select items while keeping the list open. The key-strokes call ivy-previous-line-and-call and ivy-next-line-and-call respectively, which is what I used in my custom keymap to achieve the desired behavior.

(ivy-read "Choose: "
          (list "one" "two" "three")
          :keymap (let ((map (make-sparse-keymap)))
                    (define-key map (kbd "<up>") 'ivy-previous-line-and-call)
                    (define-key map (kbd "<down>") 'ivy-next-line-and-call)
                    (define-key map (kbd "C-p") 'ivy-previous-line-and-call)
                    (define-key map (kbd "C-n") 'ivy-next-line-and-call)
          :action (lambda (n)
                    (do-something n)))


And then later I came across some code that lets me do that without those key-bindings, and with just using :update-fn. It needs to read the information in a different way though.

(defun do-something-also ()
  (message (ivy-state-current ivy-last)))

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.