I would like to programmatically call an ivy or counsel function and then immediately execute ivy-occur to get the ivy-occur buffer.

Instead of doing M-x counsel-describe-function and C-c C-o manually to get the ivy-occur buffer I would to do this with a function.

Tried the following (the last three solution work but there has to be a better way):


Works for some (counsel-M-x) but most commands give me an ivy-occur buffer with 0 candidates. So this is probably called too early:

      (add-hook 'minibuffer-setup-hook #'ivy-occur)
      (ignore-errors (counsel-faces)))
  (remove-hook 'minibuffer-setup-hook #'ivy-occur))


Same problem as with the hook.

    (:append #'ivy-occur)


This actually works, but there has to be a better way to do this. This just simulates the keys pressed:

(execute-kbd-macro (kbd "M-x counsel-faces RET C-c C-o"))

;; Can even change the initial input like this (`ivy-`).
(execute-kbd-macro (kbd "M-x counsel-faces RET ivy- C-c C-o"))


This works as well but is sort of strange too. Not even sure if this might be run before calling counsel-faces in some cases.

  (run-with-idle-timer 0.01 nil #'ivy-occur)


Found this in the question Clean way to perform commands in the Emacs minibuffer. But this also feels akward.

  (setq unread-command-events
      (append (listify-key-sequence (kbd "C-c C-o")) unread-command-events))

Thanks for any solution or a hint on how to do this properly.

1 Answer 1


Finally found a working solution that feels ok:

run-at-time with the argument nil will run the code as soon as possible, which is almost exactly after we're back into the command loop.

  (run-at-time nil nil #'ivy-occur)

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.