0

Q: how do I fold the current org-mode block where my cursor is inside of it?

I want to fold block under the current heading only. I know that I can use org-hide-block-all to fold all blocks in the buffer, and org-hide-block-toggle to toggle the current block (i.e., the one at point). But how about folding one block under the current heading?

For example:

* header 1
  Some text here 
* header 2
  Some text here [point/cursor]
* header 1
  Some text here 

I'd like to be able to fold the text under header 2 but leave the rests of headers 1 and 3 alone. And if the pointer/cursor happens to be on the header 2 instead of text below, it will also give the same behaviour. How would I do that?

1 Answer 1

1

M-x org-fold-hide-entryshould do it (and you can bind it to a key if you like). C-h f org-fold-hide-entry says:

Hide the body directly following this heading.

That's a bit misleading perhaps: it can be taken to imply that you have to be on the heading, but AFAICT it works fine from anywhere in the entry.

EDIT: Here is a toggle function:

  (defun my/org-fold-toggle-entry ()
    (interactive)
    (save-excursion
      (when (org-at-heading-p)
        (org-end-of-line))
      (if (org-fold-folded-p)
          (org-fold-show-entry)
        (org-fold-hide-entry))))

BTW, even without a toggle function, you can expand a heading using org-cycle (bound to TAB), although you may have to move the cursor to the header (e.g. if it is on the ellipsis under which the contents of the entry are hidden). So C-a TAB should do it.

4
  • Thank you for your answer. However, it's not the perfect solution because when the entry is folded and the cursor on the header, it doesn't toggle to open. Otherwise it would be perfect. org-toggle-fold doesn't close the entry. So I guess there might be a custom method needed to detect if the header/entry is closed, then open otherwise. Any suggestion?
    – ReneFroger
    Commented Jan 22 at 18:13
  • 1
    I added a toggle function because it was simple enough to do in this case and I had some time available, but note that: 1) you did not ASK for a toggle function in your question; you added that requirement in a comment after the answer was provided; 2) you should make the question complete when you ask it, not modify it later (not even in a comment); 3) if you ask a question, somebody answers the question as asked but then you decide that you wanted to ask a different question, instead of modifying the question (which is an absolute no-no) ...
    – NickD
    Commented Jan 24 at 2:59
  • 1
    ... or ask an augmented or plain different question in a comment, please accept the answer to the question as is and create a new question, maybe referring to the original question for context.
    – NickD
    Commented Jan 24 at 2:59
  • 1
    You are entirely right, I was perhaps not very clear in my question that I would use the same function for when the cursor is on the header too. I'm very happy with the solution that you have provided as it worked very well for my purproses. I really appreciate your helpful response and thinking in this, thanks for that!
    – ReneFroger
    Commented Jan 26 at 13:15

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.