In an org file, I got things like:

  • A
  • aaa
  • B
  • bbb

I'd like to save a section of it regularly to a file. The section is being updated daily.

Is there a command that could be inserted in this section to allow to perform this operation repeatedly?

For example, executing:

    * B
    (save-section to `~/b_section.org`)
    - bbb

would perform this task. That is, not just appending, but saving the new updated version of the section.

I tried to archive but, it was an incremental saving, and the section was deleted (this is a minor issue, since I could undo it). It also did not allow for selectively saving different sections to different files.


You can achieve this, at least most of the ways, using Org's export functionality. By default, the Org export dispatcher is bound to C-c C-e; hitting that will allow you to select "export as org," to a file. (N.B., you can export to whatever format pleases you.) A PROPERTIES drawer on the heading you want to export specifying the EXPORT_FILE_NAME property will control which file gets overwritten each time you export.

From here, the details get... one could say messy, or one could say "very customizable." There are probably ways to automate this fully, but I frankly don't know what they are. My preferred way to call this manually would be to put point on the top-level heading to be exported, hit C-c C-e, and then use the C-s toggle to use "subtree export" -- this will export only the current subtree, leaving the rest of your file intact. You could potentially look in to Org's "sparse tree" functionality, which would allow you to tag a heading, search for it, and only export that heading, or you could use "narrowing" along with the "export visible" dispatcher toggle.

...but I would, myself, go with C-c C-e, C-s to export only the current subtree. You can set the property with C-c C-x p.


One (imperfect but built-in) possibility is to tangle the section as a src block:

#+begin_src org :tangle your_file_name.org
  ,* B
     some content
  ,* bbb
     another content

Then doing C-c C-v t will write everything between the src block into the org file "your_file_name.org". You can edit the org src block with `C-c `` so that Emacs is doing the "," escaping of headlines for you. I guess for shorter section this might help you.

  • Sorry, that looks clumsy and not very practical. Still no -1 because the intention was good. – Quora Feans Apr 6 '18 at 16:21

Here's a function from this pragmatic emacs entry (see the comment by Jay Dresser) that might do the trick for you. It will export a heading with a particular property on save. If you'd didn't like that I'm sure you could adjust the hook to suit you.

(defun save-headings-external ()
  "Save all headings containing special property to an external file"
  (when (string= major-mode "org-mode")
  (goto-char (point-min))
  (let (-extfname -start -end)
  (while (re-search-forward ":PROPERTIES:" nil t)
  (when (setq -extfname (org-entry-get (point) "SAVE-TO-FILE"))
  (setq -start (point))
  (org-forward-heading-same-level 1 t)
  (write-region -start (point) -extfname)
  (message "Wrote '%s'" -extfname)))))))

  (add-hook 'write-file-hooks 'save-headings-external)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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