0

What is the key combination to jump directly to the end of BODY of a headline in org-mode?

* Headline title A from here ▮

text text text
text text text
text text text
text text text
text text text
to HERE▮

* Headline title B
2
  • Using C-e cursor move to end of the headline title * Headline title HERE ▮ and not to the end of HEADLINE-BODY: ...text text text text text text to HERE▮
    – Paolo
    Commented Feb 20, 2023 at 13:35
  • Sorry - misread the question.
    – NickD
    Commented Feb 20, 2023 at 14:15

2 Answers 2

4

I'm not aware of a function that does this (someone will correct me if there is), but you can write your own by getting the :contents-end property from an org element and going to that buffer location. Something similar to this:

(defun my/org-goto-end-of-contents ()
  (interactive)
  (let* ((elem (org-element-at-point))
         (end-pos (org-element-property :contents-end elem)))
    (goto-char end-pos)))

The important part being (org-element-property :content-end elem), which returns the value you're looking for. (Take a glance these two functions with C-h f: org-element-property and org-element-at-point)

However, if you want this function to respect the empty line and the end of the buffer, you can spice it up by looking for blank lines, headings, etc:

(defun my/org-goto-end-of-contents ()
  (interactive)
  (let* ((elem (org-element-at-point))
         (end-pos (org-element-property :contents-end elem)))
    (goto-char end-pos)
    (if (and (or (looking-at-p "[[:space:]]*$") 
                 (org-at-heading-p))
             (not (eobp)))
     (left-char 1))))

Bind this function to your key of choice and you're good to go.

2
  • 3
    There is the built-in function org-end-of-subtree which seems to be equivalent to the first command here.
    – orgtre
    Commented Feb 20, 2023 at 23:33
  • I knew someone would know of a built-in function. That being said, it's fun to tinker with org-element-at-point and its properties. Thanks, @orgtre! Commented Feb 21, 2023 at 6:24
2

The commands provided by kozina-adjacent go to the end of the current subtree, as does org-end-of-subtree. However, if one has a heading with text under it followed by several subheadings, one might just want to go to the end of the text before the first subheading. For this I wrote the following command (it does as advertised with a universal argument prefix, but does other things too, and could be further improved):

(defun org-colviewx-beginning-of-contents (&optional arg)
  "Go to first non-whitespace character of entry content.
Skips meta-data. When the entry content is only meta-data and
whitespace, go to the first subheading; but if there is no
subheading go to the end of of the last non-meta-data line of
entry, inserting it if necessary.

With a `\\[universal-argument]' prefix argument go to last \
non-whitespace
character instead. With a `\\[universal-argument] \
\\[universal-argument]' prefix argument go to
last non-whitespace character in subtree."
  (interactive "P")
  (let ((initial-level (org-current-level)))
    (org-end-of-meta-data t)
    (if (and (org-at-heading-p)
             (= initial-level (org-current-level)))
        (progn (backward-char)
               (when (save-excursion
                       (backward-char)
                       (looking-at-p "[^ \n\t]"))
                 (newline)))
      (cond
       ((equal arg '(4))
        (if (re-search-forward "[ \n\t]*\n\\*" nil t)
            (goto-char (match-beginning 0))
          (org-end-of-subtree)
          (re-search-backward "[^ \n\t]")
          (goto-char (match-end 0))))
       ((equal arg '(16))
        (org-end-of-subtree t)
        (re-search-backward "[^ \n\t]")
        (goto-char (match-end 0)))))
    (when (called-interactively-p 'interactive)
      (org-fold-show-context))))
2
  • Thank you! I was looking for just what the org-end-of-subtree function does!
    – Paolo
    Commented Feb 21, 2023 at 3:43
  • If one of these answers has answered your question satisfactorily, please accept it.
    – NickD
    Commented Feb 22, 2023 at 2:21

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.