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?;)
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.
Sign up to join this communityAssumption: 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.
outline-next-heading
in order to deal with a folded heading with subheadings. The original version did not work correctly in that case.
M-w
.