I'd like to be able to do the following:

* A story about airplanes
** Chapter one
*** # Introduce the main character
Billy was a nice boy etc ...
*** # Set the scene
One day Billy stumbled upon a paper airplane..

In other words, I want to be able to write prose and use headings with their expand/collapse capability to give hints to myself about what I'm trying to say in a given paragraph/section, but when exporting I don't want these to be treated as headings that should be part of the text.

Is this possible?

According to this page[1] lines starting with # are treated as comments. But when they are headings they are still exported.

[1] https://orgmode.org/manual/Comment-lines.html


1 Answer 1


Okay, I figured it out:

(defun strip-comment-line (str)
  ; <space> <star>* <space> <hash> <anything> <eol>
  (replace-regexp-in-string "[ ]*[*]*[ ]*#.*$" "" str))

(defun org-remove-comment-headlines (backend)
  "Remove headlines that are comments starting with '#'."
  ; traverse all headings
    ; call this lambda on each heading
    (lambda ()
      ; re-insert it into the buffer at the current position
        ; if it's a comment line strip it
          ; extract current line from buffer
          (delete-and-extract-region (point-at-bol) (point-at-eol)))))

(add-hook 'org-export-before-processing-hook #'org-remove-comment-headlines)
  • Any reason you use delete-and-extract-region instead of just replace-regexp limited to (point-at-bol) (point-at-eol) ?
    – rpluim
    Commented Sep 19, 2018 at 12:01
  • I didn't know about replace-regexp. All the search/replace type functions I could find operate on strings, so I had to use delete-and-extract-region as a way to obtain the string I needed.
    – numerodix
    Commented Sep 20, 2018 at 12:04

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