8

I've an huge org file HUGE.org with this structure:

* ID-HEAD-1 Title1 :TAG:
bla bla bla

* ID-HEAD-2 Title2 :TAG:
bla bla bla

...

* ID-HEAD-N TitleN :TAG:
bla bla bla

and I want to split HUGE.org into:

ID-HEAD-1.org
ID-HEAD-2.org
...
ID-HEAD-N.org

where the content of each ID-HEAD-i.org is:

* ID-HEAD-i Titlei :TAG:
bla bla bla

Suggestions?

2
  • Clarification: you mean you want to create N new files, each of which contains the sequentially numbered top-level header and its contents?
    – Dan
    May 5, 2016 at 17:45
  • Yes, i want N new files!
    – lap
    May 5, 2016 at 17:53

2 Answers 2

7

I use a function of my own. It moves a subtree to a new org file, and replaces it with a link to this new file. Specify the name of the file through the parameter or interactively at the prompt.

(defun org-move-tree (filename)
  "move the sub-tree which contains the point to a file,
and replace it with a link to the newly created file"
  (interactive "F")
  (org-mark-subtree)
  (let
      ((name (buffer-substring (region-beginning)
                               (save-excursion (end-of-line) (point))))
       (xxx (buffer-substring (region-beginning) (region-end))))
    (setq name (replace-regexp-in-string "^[*]+ *" "" name))
    (delete-region (region-beginning) (region-end))
    (insert (format "[[file:%s][%s]]\n" filename name))
    (find-file-other-window filename)
    (insert xxx)
    (save-buffer)))

(provide 'org-move-tree)
1

I tried to use the previous answer as is but I had to tweak it a bit to make it work as I would expect. I think this version might be slightly safer too because it should handle tags correctly.

(defun y/org-move-tree (buffer-file-name)
  "move the sub-tree which contains the point to a file,
and replace it with a link to the newly created file"
  (interactive "F")
  (org-mark-subtree)
  (let*
      ((title    (car (last (org-get-outline-path t))))
       (dir      (file-name-directory buffer-file-name))
       (filename (concat dir title ".org"))
       (content  (buffer-substring (region-beginning) (region-end))))
    (delete-region (region-beginning) (region-end))
    (insert (format "** [[file:%s][%s]]\n" filename title))
    (with-temp-buffer
      (insert content)
      (write-file filename))))

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.