In writing documents using org mode, I organize contents that are not supposed to show up in the final product into sections with an :noexport: attribute. These are mostly notes and auxiliary code blocks. So, I often fold each of them manually so that I can see the main text.

Is there a way to configure Emacs to automatically fold/hide these noexport sections when any .org file is initially loaded?

(This is with Emacs 27 under Ubuntu 20.04 LTS, org-mode 9.4.6).

  • 2
    Initial visibility is normally controlled via the VISIBILITY property. Maybe setting that manually is OK in your case.
    – Juancho
    Apr 7, 2022 at 11:37
  • I would recommend @Juancho's solution, although there are (more complicated but less manual) alternatives. E.g. you can add a function to the find-file-hook that checks for Org mode files first and then uses org-map-entries to find entries with a noexport tag and fold them with org-cycle.
    – NickD
    Apr 7, 2022 at 13:13

1 Answer 1


Here is the (more complicated but less manual) solution I mentioned in my comment.

Here's an Org mode file with some headlines marked noexport and others not. It also includes a noexport code section with two code blocks: the first one defines a function and adds it to the find-file-hook and the second cleans up the hook if you decide you don't like it. To see it in action, open the file in Emacs, evaluate the first code block with C-c C-c, and then close the file and reopen it: the noexport sections (and only those) should be folded:

#+STARTUP: showall

* foo

* bar                                                         :noexport:

* baz

* Code                                                        :noexport:

#+begin_src elisp :results drawer
  (defun ndk/org-fold-noexport-sections ()
    (org-map-entries #'org-cycle "noexport" 'file))

  (add-hook 'find-file-hook #'ndk/org-fold-noexport-sections)

#+begin_src elisp :results drawer
  (remove-hook 'find-file-hook #'ndk/org-fold-noexport-sections)

The #+STARTUP: showall keyword makes sure that Org mode will start in a known folding state: with everything unfolded. When the hook is run and the function is evaluated, it is going to walk the file, visiting every headline that matches the noexport tag and it will run org-cycle on it: since it is open to begin with, that will fold it. Another way to do that is through the variable org-startup-folded: the default value showeverything is equivalent to the #+STARTUP: setting above, but it applies to every Org mode file, so you don't have to worry about setting it per-file.

You can clean up by running the second code block with C-C C-c: that will delete the function from the find-file-hook. OTOH, if you are happy with it, copy the first code block into your Emacs init file.

@Juancho's solution (see his comment above) is simpler (but more manual); it's also using built-in Org mode facilities only, so there is no extra baggage (like the code block above) needed.

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.