I want to set a key binding that creates a sparse tree of all headlines containing the string currently on the kill-ring.

Currently, I mark the text I want and save it to the kill-ring, then call org-sparse-tree to create a sparse tree from a regular expression (C-c / r). When prompted for the regexp, I yank the text in the kill-ring.

I would like to do this without calling an interactive function. If I already have my regexp in the kill-ring, just pressing a key combination would save me much time.

I have tried to pass arguments to org-sparse-tree, but this function doesn’t seem to work this way (it is interactive).

  • 2
    You have to go a level down to find out what it is doing. If you look at the source code for org-sparse-tree (do C-h f org-sparse-tree and then click on the org.el link at the end of the top line), you will see that in the r case, it calls (interactively) the function org-occur. So do C-h f org-occur to find out how to use it and then write your own function that grabs the regexp out of the kill ring and then calls org-occur with that regexp as argument. And if you make it a [command]() then you can bind it to a key of your choice.
    – NickD
    Commented Jul 11, 2023 at 22:38
  • @NickD: Very helpful comment.
    – Drew
    Commented Jul 12, 2023 at 3:01
  • I forgot to add the link: command - sorry about that.
    – NickD
    Commented Jul 12, 2023 at 12:27

1 Answer 1


Thanks NickD for pointing me in the right direction. I wrote the following function that does what I need:

(defun kill-ring-sparse-tree ()  
  (let ((regexp (car kill-ring)))
    (org-occur regexp)))
(global-set-key (kbd "C-c b") 'kill-ring-sparse-tree)
  • FYI, you can accept your own answer.
    – Drew
    Commented Jul 12, 2023 at 3:00

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.