0

I want to copy the contents of a heading, but only the content and not the heading and also not any levels deeper;)

Is there any function made for this or do I have to start somehow with org-mark-subtree?;)

2
  • It is just text, so you can cut and paste as in any other buffer: mark the beginning of the region, move to the end, and do M-w.
    – NickD
    Mar 21 at 16:26
  • I mean a function to do it;) Mar 22 at 7:07

1 Answer 1

1

Assumption: point is on the heading or in the text of that heading.

The following function will copy the text of that heading (up to but not including the next heading, whether that next heading is higher, at the same level or lower in the hierarchy). It does the equivalent of what I suggested in my comment:

#+begin_src elisp
  (defun my/copy-text-of-current-heading ()
    (interactive)

    (save-excursion
      ;; move back to heading if not already there
      (org-back-to-heading)
      (let* ((context (org-element-at-point))
             (beg (org-element-property :contents-begin context))
             (end (save-excursion (outline-next-heading) (- (point) 1))))
        (kill-ring-save beg end))))

  (define-key org-mode-map (kbd "C-c z") #'my/copy-text-of-current-heading)

#+end_src

Assuming you are on the heading of interest or somewhere in the text below it, press C-c z: that will copy the text up to the next heading to the kill ring. You can retrieve it as usual with C-y (bound to yank). Other than that, the function has no other side effects. In particular, it does not disturb point. The function works the same way whether the heading is folded or not.

1
  • I updated the function to use outline-next-heading in order to deal with a folded heading with subheadings. The original version did not work correctly in that case.
    – NickD
    Mar 22 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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