22

I'd like to perform one simple task but there seems to be no clear solution for it:

I have a structure of subtrees like this:

* Tree A
** Subtree A1
** Subtree A2
* Tree B
** Subtree B1
** Subtree B2

I'd like to set up one key shortcut to move subtrees from Tree A to Tree B. Moving to the archive seems to be easy, but do you have any clue how to do it within one file?

Any help appreciated! Thanks!

2
  • In order for refile sub tree to work (C-c C-w) your indents need to indent by 2. e.g. *** SubtreeA1. I had my indents setup like yours and couldn't get it working until I realized this.
    – 79E09796
    Jul 24, 2013 at 12:58
  • 2
    This question might be of help. You can (temporarily) add the current file to org-refile-targets with a :maxlevel of >= 2 set whenever you refile. However, my lisp-fu is not strong enough yet to help you with that. Jan 7, 2016 at 15:07

6 Answers 6

17

You could try to refill the subtree with C-c C-w (it work also to move the subtree to another file).

Another way is to fold the subtree, then to kill it with C-k C-k and to paste it where it should be.

The third solution I use some time is to change the level of the subtree with M-left, then to move it with M-up and M-down, then M-right put it again on the correct level. This third way have the shortcoming that some time it make other part of the tree to move.

6

You can refile the subtree using C-c C-w, depending on how you have refile set up. Depending on the depth of your destination you may not be able to use it as a valid destination.

You can kill or copy a subtree without having to fold the structure using the kill/copy subtree commands: C-c C-x C-w and C-c C-x M-w respectively, yanking a subtree is either C-c C-x C-y or simply C-y

6

I also wanted a way for org-refile to refile easily to a subtree, so I wrote some code and generalized it so that it will set an arbitrary immediate target anywhere (not just in the same file).

Basic usage is to move somewhere in Tree B and type C-c C-x C-m to mark the target for refiling, then move to the entry in Tree A that you want to refile and type C-c C-w which will immediately refile into the target location you set in Tree B without prompting you, unless you called org-refile-immediate-target with a prefix arg C-u C-c C-x C-m.

Note that if you press C-c C-w in rapid succession to refile multiple entries it will preserve the order of your entries even if org-reverse-note-order is set to t, but you can turn it off to respect the setting of org-reverse-note-order with a double prefix arg C-u C-u C-c C-x C-m.

(defvar org-refile-immediate nil
  "Refile immediately using `org-refile-immediate-target' instead of prompting.")
(make-local-variable 'org-refile-immediate)

(defvar org-refile-immediate-preserve-order t
  "If last command was also `org-refile' then preserve ordering.")
(make-local-variable 'org-refile-immediate-preserve-order)

(defvar org-refile-immediate-target nil)
"Value uses the same format as an item in `org-refile-targets'."
(make-local-variable 'org-refile-immediate-target)

(defadvice org-refile (around org-immediate activate)
  (if (not org-refile-immediate)
      ad-do-it
    ;; if last command was `org-refile' then preserve ordering
    (let ((org-reverse-note-order
           (if (and org-refile-immediate-preserve-order
                    (eq last-command 'org-refile)) nil org-reverse-note-order)))
      (ad-set-arg 2 (assoc org-refile-immediate-target (org-refile-get-targets)))
      (prog1 ad-do-it
        (setq this-command 'org-refile)))))

(defadvice org-refile-cache-clear (after org-refile-history-clear activate)
  (setq org-refile-targets (default-value 'org-refile-targets))
  (setq org-refile-immediate nil)
  (setq org-refile-immediate-target nil)
  (setq org-refile-history nil))

;;;###autoload
(defun org-refile-immediate-target (&optional arg)
  "Set current entry as `org-refile' target.
Non-nil turns off `org-refile-immediate', otherwise `org-refile'
will immediately refile without prompting for target using most
recent entry in `org-refile-targets' that matches
`org-refile-immediate-target' as the default."
  (interactive "P")
  (if (equal arg '(16))
      (progn
        (setq org-refile-immediate-preserve-order
              (not org-refile-immediate-preserve-order))
        (message "Order preserving is turned: %s"
                 (if org-refile-immediate-preserve-order
                     "on" "off")))

    (setq org-refile-immediate (unless arg t))
    (make-local-variable 'org-refile-targets)
    (let* ((components (org-heading-components))
           (level (first components))
           (heading (nth 4 components))
           (string (substring-no-properties heading)))
      (add-to-list 'org-refile-targets
                   (append (list (buffer-file-name))
                           (cons :regexp
                                 (format "^%s %s$"
                                         (make-string level ?*)
                                         string))))
      (setq org-refile-immediate-target heading))))

(define-key org-mode-map "\C-c\C-x\C-m" 'org-refile-immediate-target)

It was hard to find a key that was free on the C-c C-x prefix, so I used m with the mnemonic i*mm*ediate

1
  • I am working on automating this for multiple todo and I already have a working draft of lawlist-auto-refile: superuser.com/questions/613968/… It sure would be helpful if aculich or some other maven could please create a variable similar to (setq org-archive-location "~/0.todo.org::* Archived Tasks") so we could specify the file and heading, which is already a part of the org-archive-subtree functionality. I'm doing a search and mark because I don't have the wherewithal to create something like org-archive-location for this setup.
    – lawlist
    Jul 2, 2013 at 6:27
4

I use M-left, M-up/down and then M-right if the structure of the file is not too complex. But keep in mind that if you try to move Subtree A1 to Tree B using this method, you will lose its child Subsubtree A1.1, unless you move it with M-S-left/right:

* Tree A
** Subtree A1             <=== move this heading with M-S-left
*** Subsubtree A1.1       <=== or you will leave this heading behind
** Subtree A2

* Tree B
** Subtree B1
** Subtree B2
4

Moving a subtree within the same buffer can be done with org-refile. While moving, you can even change hierarchy of the original (number of stars). However, it might not work out of the box primarily because the buffer you are working with is not in the org-refile-targets. You could add the file in the refile targets by using C-[, then run org-refile with C-c C-w and finally remove the file from the refile targets with C-]. This might be too much (and might fail if your maxlevel setting is too low - which you can change anyway). Alternatively, to automate this procedure in one swoop, you will first have to define a function like this:

(defun refile-in-current ()
"refile current item in current buffer"
(interactive)
(let ((org-refile-use-outline-path t)
    (org-refile-targets '((nil . (:maxlevel . 5)))))
(org-refile)))

The nil in there means "current buffer". The let redefines your variables locally only. You will then have to add a shortcut to run the function. Here is one way to bind it to C-c m:

(add-hook 'org-mode-hook (lambda ()
        (local-set-key "\C-c m" 'refile-in-current)
0
1
(require 'org-archive)                                                                               
(setq org-archive-save-context-info nil)                                                             
(setq org-archive-location "::* Archived Tasks")

run (org-archive-subtree) at subtree which you want to move, it will be moved into "* Archived Tasks" heading

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.