Q: How can I tell isearch to ignore the contents of folded blocks by default in org-mode buffers?

More specifically, I'd like isearch to ignore contents of comment blocks and drawers: When authoring documents in org-mode I often use comment blocks and custom drawers to store different kinds of notes about the text I am writing. I keep them folded by default to make sure their contents don't distract me:

(add-hook 'org-mode-hook #'org-hide-block-all)

This works really well, but won't stop org-mode from expanding these blocks if they contain matches for a string I am isearching.


You can set search-invisible to nil in your .emacs or using Customize. Then, if you do want to show matches in invisible text, press M-s i while using isearch.

(I found this by reading the docs of isearch-forward and isearch-toggle-invisible, but it is also documented in the manual: see Special Isearch.)

This affects all buffers, not just Org; if you want to use nil in org-mode buffers and the default elsewhere you can create a buffer-local binding in Org buffers:

(add-hook 'org-mode-hook
          (lambda ()
            (make-local-variable 'search-invisible)
            (setq search-invisible nil)))
  • 1
    A buffer-local value would be way more practical than a let binding advice. :-)
    – Malabarba
    Nov 28 '14 at 0:28
  • @Malabarba: Very true! (I didn't know that one can use make-local-variable to create a buffer-local binding for a variable that is already defined as global -- until now, that is. I'm still learning stuff. :-) ) I'll update the answer. Nov 28 '14 at 4:17

While using isearch you can toggle the search on invisible text with M-s i.

You can also customize this behaviour with M-x customize-group isearch and searching for Search Invisible.

Note that this will ignore everything in a folded block, I can't ignore only the text in a commented block.

  • The two of us must have been typing at the same time! :-) Nov 27 '14 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.