I'm using the Outline minor mode. (Or the major mode or Org mode, but in these modes the scenario in this question is rare.) I want to show all of the headings that have no parent, plus the text before the very first heading.

In simple cases, this just means showing level 1 headings. But when a file starts with a larger-level heading, I want to show this larger-level heading, and all headings at the same level until the first heading at a smaller level, and so on. If the file starts with a non-heading line, I want to show all the text up to the first heading. For example consider the following LaTeX file meant to be included in a larger document:

%% This file contains stuff.
%% It starts with a comment.
There's stuff here, then more stuff.

I want a command that shows this and hides the rest:

%% This file contains stuff.
%% It starts with a comment.

This is different from hide-sublevels which shows headings up to a given level.

The idea is to show only the minimum set of headings such that the whole document is somewhere under one of the visible headings, except for the first entry before the first heading, which remains visible. All entries (except for that top entry) and all headings that have a parent should be hidden.

(I'll use this to replace hide-sublevels in outline-focus, so as to show parentless headings, plus all the headings on the path to the point, à la hide-other.)

Does this exist in Outline or Org, or do I need to roll my own?


2 Answers 2


The following command hides parentless subtrees, and leaves text before the first header visible:

(defun outline-hide-parentless-subtrees ()
  "Hide subtrees below parentless headings."
    (goto-char (point-min))
    ;; edge case: is the first line a header?
    (if (outline-on-heading-p)
    ;; cycle through headings, hiding successively higher ones
    ;; watch out for headers on last line, which would get us stuck
    (while (and (progn (outline-next-visible-heading 1)
                (< (point-at-eol) (point-max)))

I've tested this function lightly, and it seems to work as advertised. There may be some edge cases that didn't occur to me.


Difficulty is how to show all first entry before the first heading. Seems outline-mode is based on regular expressions only. You could hard-code some lines before headers-match into outline-regexp. Something like

"^[^\n]+\n\\\\[^ ]+"

which should include one line above.

Determining a form with an unknown number of lines above would require a function. It should be possible to extend outline-next-visible-heading and relatives taking an optional function-argument: when delivered, do not stop at match-beginning, run the fun.

  • 1
    I don't understand the point you're trying to make here. I can, with Outline mode, show/hide the entry before the first heading. Jun 4, 2015 at 11:14

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.