Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In Emacs org-mode, is there a command like new-file-from-subtree?

As I envision it, this command would cut the subtree from the current file, paste the subtree into a new buffer, then save the new buffer with the name of the subtree.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

I don't believe such a command currently exists. However the following will do the trick:

(require 'org-element)

(defun zin/org-file-from-subtree (&optional name)
  "Cut the subtree currently being edited and create a new file
from it.

If called with the universal argument, prompt for new filename,
otherwise use the subtree title."
  (interactive "P")
  (org-back-to-heading)
  (let ((filename (cond
                   (current-prefix-arg
                    (expand-file-name
                     (read-file-name "New file name: ")))
                   (t
                    (concat
                     (expand-file-name
                      (org-element-property :title
                                            (org-element-at-point))
                      default-directory)
                     ".org")))))
    (org-cut-subtree)
    (find-file-noselect filename)
    (with-temp-file filename
      (org-mode)
      (yank))))

As is, it will not promote the current heading to level 1, it will retain the existing depth. (Promotion should be doable as well, but would require more complex code). It also gives the option of prompting for a new filename, using C-u.

share|improve this answer
    
Awesome! Any way to make it append .org to the filename? What about making it return me to the original buffer I was working from? –  Peter Salazar Mar 19 '13 at 17:31
    
Also I tried to add a keybinding but it didn't work. Clearly I'm doing something wrong. (add-hook 'org-mode-hook (lambda () (local-set-key "C-c C-x n" 'my-zin-org-new-file-from-subtree))) –  Peter Salazar Mar 19 '13 at 17:35
1  
Added the ".org" appending to the filename when created from the headline title. It will also now open the new file in background but not change buffers. –  Jonathan Leech-Pepin Mar 19 '13 at 19:29
1  
For the keybinding, change it to "\C-c\C-xn" and it should work. –  Jonathan Leech-Pepin Mar 19 '13 at 19:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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