Reputation: 11
In org-mode I need to quickly copy content under a heading to the kill ring excluding the heading itself. I found the function org-copy-subtree
, but it copies the entire subtree with the heading.
In the following example I would like to copy "Sample text line 1" and "Sample text line 2" when my cursor is somewhere in the "My Sample Heading 1":
* My Sample Heading 1
Sample text line 1
Sample text line 2
* My Sample Heading 2
Sample text line 3
Sample text line 4
Is there a way to do it?
Upvotes: 1
Views: 1081
Reputation: 31
This might be a bit of a dead question, but here's my solution in case anyone happens upon this and wants to use the org-mark-subtree
way of doing it:
(defun my/org-copy-text-under-heading ()
(interactive)
;; Select the current Org subtree
(org-mark-subtree)
;; Deselect the heading line
(next-line 1)
;; Copy the current region
(kill-ring-save
(region-beginning)
(region-end))
;; Deselect the region after copying
(deactivate-mark))
I've just tried to explain that answer a little more fully (as I assume the jy
will only work with Evil Mode?).
Upvotes: 3
Reputation: 508
This will return a substring of everything under the current headline. If you want to kill the contents directly, just replace buffer-substring-no-properties
with kill-region
.
(save-excursion
(org-back-to-heading)
(forward-line)
(unless (= (point) (point-max))
(let ((b (point))
(e (or (outline-next-heading) (point-max))))
(buffer-substring-no-properties b e))))
Upvotes: 1