samlaf
samlaf

Reputation: 505

Emacs org-mode delete contents of a tree (or copy structure of tree without content)

I am trying to make a Natural Planning Tree (following advices from Getting Things Done by David Allen) which looks something like:

* Natural Planning Model

** ITEM 1
*** Purpose and Principles (Why)
*** Outcome Visioning
*** Brainstorming
*** Organizing
*** Identifying next actions

** ITEM 2...
** ITEM 3...

I would like to copy the outline structure of ITEM 1 to new ITEMS. But the problem is that I have already filled a lot of information inside the subtree for ITEM 1.

My general question is: Is there a way to delete the contents of a tree but not it's headers (the rows that start with *)? Similarly, is there a way to copy the structure of a tree without it's contents?

Upvotes: 5

Views: 925

Answers (1)

Kyle Meyer
Kyle Meyer

Reputation: 1586

I don't recall a command that does this in Org, but you could make your own pretty easily.

(defun org-copy-subtree-headings-as-kill ()
  "Copy headings of current subtree as kill."
  (interactive)
  (save-excursion
    (org-back-to-heading)
    (let* ((el (org-element-at-point))
           (beg (org-element-property :begin el))
           (end (org-element-property :end el))
           (tree (buffer-substring-no-properties beg end)))
      (with-temp-buffer
        (insert tree)
        (goto-char (point-min))
        (while (not (eobp))
          (if (looking-at-p "^\\*")
              (forward-line)
            (delete-region (point-at-bol) (1+ (point-at-eol)))))
        (kill-new (buffer-string))))))

Also, if these headings are often the same set of headings, you could use a yasnippet template.

To delete the content from the current tree (instead of copying the headings), you could just narrow to the subtree and call keep-lines with ^\*.

Upvotes: 2

Related Questions