14

I'm just getting started with org-mode and imagine a workflow whereby I capture new tasks and notes in an org-mode "inbox" file. For tasks that grow into projects, I would imagine refiling them into a new org-mode file which is then added to my agenda.

The org-mode manual describes refiling a tree mostly the way I'm looking for, except that it only seems to allow refiling the section within the same file. What I'd like to do, is take that tree, and refile it into a new file (and then preferably add the file to the agenda list).

I can copy-paste into a new buffer, and then add the new buffer to the agenda, but it seems like this is the sort of thing that the org-mode gods would have a shortcut for.

4
  • Why do you want to make a new file for every project? There are small projects and big ones, so you can have a projects.org file as a sort of incubation file which contains all projects initially and if a project grows so big that it deserves its own file then you can create it manually.
    – Tom
    Jan 1, 2012 at 20:33
  • Thanks, Tom. That's my thinking. Big things move to their own file when they get too big. I found a flag that lets me refile to another file in my agenda list, but only under an existing heading. I'm going to do a little more digging to see if I can simply add a tree to a new empty org file...
    – Peter
    Jan 1, 2012 at 21:17
  • 1
    @Peter you can refile any tree into another file. but first you need to add destination file to org-agenda-files
    – kindahero
    Jan 1, 2012 at 21:36
  • @kindahero: Is that a default option? I have a file "test1.org" with a tree that I want to move to an empty file "test2.org" that has been saved and added to the agenda with C-c [. The only refile targets that it gives me as completion options are from "test1.org."
    – Peter
    Jan 1, 2012 at 23:15

2 Answers 2

2

Here is a fast-and-filthy elisp function that could be modified as you may wish.

(defun subtree-to-new-file ()
  (interactive)
  "sloppily assists in moving an org subtree to a new file"
  (org-copy-subtree nil t)
;;; This long setq statement gets the title of the first heading, to use as a default filename for the new .org file.
(setq first-heading
  (with-temp-buffer
    (yank)
    (beginning-of-buffer)
    (search-forward " " nil nil 1)
    (setq title-start (point))
    (end-of-visual-line)
    (setq title-end (point))
    (setq first-heading (buffer-substring title-start title-end))
  ))
(setq def-filename (concat first-heading ".org"))
(let ((insert-default-directory t))
  (find-file-other-window  
    (read-file-name "Move subtree to file:" def-filename)
  ))
(org-paste-subtree)
;;; this final command adds the new .org file to the agenda
(org-agenda-file-to-front)
)

You can give this code a quick try by pasting into your *scratch* buffer and hitting Ctrl+j. Then go to a subtree in an org-mode file and hit Alt+x to M-x subtree-to-new-file.

If you want it to be in place every time you use emacs and are completely unfamliar with elisp, the easiest thing might be to also paste this code somewhere into your .emacs configuration file and save it. You can also add a line before or after the function to give it a keybinding. The easiest way to do that (but maybe not the best) would be something like: (global-set-key "\C-xw" 'subtree-to-new-file).

1
  • Well, I've really moved on in another direction from here, but this does the trick. I asked this a long time ago, and it's great to see an answer. Thanks for posting this--it works like a charm. Maybe I will try to remember what I was hoping to accomplish here, and start using this.
    – Peter
    Oct 14, 2013 at 18:38
5

Too add other org-mode buffers to the refile list, you will need to add a line such as follows to your .emacs file. I took this from Brent Hansen's configuration (See here)

; Targets include this file and any file contributing to the agenda - up to 9 levels deep
(setq org-refile-targets (quote ((nil :maxlevel . 9)
                                 (org-agenda-files :maxlevel . 9))))

Only (org-agenda-files :maxlevel .9) is actually needed, the other part sets a maximum number of levels to refile to.

4
  • 1
    This lets me add a tree to an existing tree in a new file, but not to a blank, newly added file with no existing content. I think this may be as close as I get.
    – Peter
    Jan 3, 2012 at 20:37
  • You might also be able to add (setq org-refile-allow-creating-parent-nodes (quote confirm)) as referenced in the same code block from Bernt (see link above). It will allow you to add them as parent nodes, not just as subheadings. I'm not sure if you can add a new file on the go that way unfortunately. Jan 3, 2012 at 21:24
  • 1
    Even with that line, the files in the agenda don't show up as refile targets. It appears I can only refile to an existing header.
    – Peter
    Jan 4, 2012 at 17:10
  • 1
    What about with (setq org-refile-use-outline-path 'file)? This may make files as targets. Nov 14, 2014 at 5:57

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.