12

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.

itsjeyd
  • 14,586
  • 3
  • 58
  • 87

2 Answers2

13

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)))
Constantine
  • 9,072
  • 1
  • 34
  • 49
  • 1
    A buffer-local value would be way more practical than a let binding advice. :-) – Malabarba Nov 28 '14 at 00: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. – Constantine Nov 28 '14 at 04:17
  • Why not use `setq-local` instead of `make-local-variable` and `setq`? – Rudolf Adamkovic Feb 22 '22 at 07:09
  • We could use `setq-local` here of course --and probably should when Emacses before 24.2 don't need to be supported-- because it is [identical to `make-local-variable` with `setq`](https://www.gnu.org/software/emacs/manual/html_node/elisp/Creating-Buffer_002dLocal.html#index-setq_002dlocal). The answer was written before wide adoptability of `setq-local`, that's all. – Y. E. Apr 01 '23 at 14:26
8

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.

MonsieurBanana
  • 488
  • 2
  • 7