10

For example, say I have a buffer that looks like this:

* Org-Mode

This is a document written in Org-Mode

** This is a subheading below that

Would it be possible to narrow the buffer to just "This is a document written in Org-Mode" and continue on then come back to the full buffer, excluding the subheading below.

(Edited to clarify regarding subheadings)

Bob
  • 173
  • 1
  • 7

2 Answers2

25

org-narrow-to-subtree (C-x n s) will display only the current heading. It does however include the heading itself, not just the text. Maybe that is OK for you?

widen (C-x n w) will widen the view again.

See for example: https://stackoverflow.com/questions/17156595/in-emacs-org-mode-how-to-narrow-display-to-two-subtrees-in-two-separate-files

Reign of Error
  • 868
  • 1
  • 6
  • 12
  • 1
    Not exactly. The problem with that approach is that if there are subheadings below that text, they're included in the narrowed in the buffer as well. Thank you, though. – Bob Apr 01 '16 at 19:05
  • 2
    Ah OK - maybe you could clarify in your question that you specifically want to exclude subheadings below the given heading – Reign of Error Apr 01 '16 at 19:06
  • also this has the problem that the indentation stays the same :( does not help on phones where there is little space... – jhegedus Jun 02 '17 at 09:00
11

Perhaps this does what you want?

(defun org-narrow-to-here ()
   (interactive)
   (org-narrow-to-subtree)
   (save-excursion
     (org-next-visible-heading 1)
     (narrow-to-region (point-min) (point))))

Edit: If you really really want to exclude the current heading, this more elaborate variant:

(defun org-narrow-to-here ()
  (interactive)
  (save-excursion
    (narrow-to-region
     (progn (unless (org-at-heading-p) (org-next-visible-heading -1))
            (forward-line)
            (point))
     (progn (org-next-visible-heading 1)
            (point)))))
Harald Hanche-Olsen
  • 2,401
  • 12
  • 15
  • To make it more robust, consider adding code to the start of the second `progn` to throw an error if `(org-at-heading-p)` is true. In this case, there is no text to narrow to between the two headings. – Harald Hanche-Olsen Apr 01 '16 at 19:56