7

imenu can be used in org-mode to quickly jump to a heading. It first prompts for the 1st level heading, and then asks for the 2nd level heading and so on untill the selected heading has no subheading and we are then taken to that last selected heading.

But what I would rather like is imenu to present the list of all headings and let me select directely the one I want to go to because

  • it would be faster
  • and it would allow jumping to a heading which has subheadings

I understand that this is not the case (at least by default) because org files may have several headings sharing the same name.

But in many situations, org headings do have unique names and therefore it would be very convinient.

How could this be done ?

2
  • 2
    not really answering your question, but a suggestion for an easier navigation option. Use helm-org-in-buffer-headings from the helm package. It lets you narrow down interactively to the desired headline, and the whole hierarchy path of the headline is used (well, the number of levels can be configured). This to me is the most efficient way to jump to a heading. Also, you can easily configure other actions besides jumping to the heading (e.g. clocking in, refiling, etc.)
    – dfeich
    Commented Mar 4, 2016 at 20:05
  • In another question folks pointed to an ivy GitHub issue about this. The consensus seems to be to (a) use org-goto and change the settings to auto-complete, or (b) use counsel-org-goto. -- No clue why counsel-imenu with org only shows leaf nodes, but maybe this helps someone else in their web search.
    – ctietze
    Commented Nov 25, 2020 at 7:07

3 Answers 3

8

Instead of using imenu, you can use org-goto with the following settings:

(setq org-goto-interface 'outline-path-completionp)
(setq org-outline-path-complete-in-steps nil)

,----[ C-h v org-goto-interface RET ]
| org-goto-interface is a variable defined in ‘org.el’.
| Its value is ‘outline-path-completionp’
| Original value was outline
|
| Documentation:
| The default interface to be used for ‘org-goto’.
| Allowed values are:
| outline                  The interface shows an outline of the relevant file
|                          and the correct heading is found by moving through
|                          the outline or by searching with incremental search.
| outline-path-completion  Headlines in the current buffer are offered via
|                          completion.  This is the interface also used by
|                          the refile command.
|
| You can customize this variable.
|
| [back]
`----

,----[ C-h v org-outline-path-complete-in-steps RET ]
| org-outline-path-complete-in-steps is a variable defined in ‘org.el’.
| Its value is nil
| Original value was t
|
| Documentation:
| Non-nil means complete the outline path in hierarchical steps.
| When Org-mode uses the refile interface to select an outline path
| (see variable ‘org-refile-use-outline-path’), the completion of
| the path can be done in a single go, or it can be done in steps down
| the headline hierarchy.  Going in steps is probably the best if you
| do not use a special completion package like ‘ido’ or ‘icicles’.
| However, when using these packages, going in one step can be very
| fast, while still showing the whole path to the entry.
|
| You can customize this variable.
|
| [back]
`----
5

Use org-imenu-depth.

See https://orgmode.org/manual/Cooperation.html

By default the Imenu index is two levels deep. Change the index depth using thes variable, org-imenu-depth.

1
  • after setting this, imenu will still require you to drill down one level at a time. I found that consult-imenu display a flat list that the OP asks (if you are using consult). for ivy user you might want to try counsel-imenu , helm user helm-semantic-or-imenu
    – user178047
    Commented Dec 15, 2023 at 18:00
2

Hoping below function would help

;;; add func to perform imenu for all headings at one level
(defun org-imenu-get-heading ()
  "Produce the index for Imenu."
  (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  (setq org-imenu-markers nil)
  (let* ((n org-imenu-depth)
         (re (concat "^" (org-get-limited-outline-regexp)))
         (subs (make-vector (1+ n) nil))
         m level head0 head)
    (save-excursion
      (save-restriction
        (widen)
        (goto-char (point-max))
        (while (re-search-backward re nil t)
          (setq level (org-reduced-level (funcall outline-level)))
          (when (and (<= level n)
                     (looking-at org-complex-heading-regexp)
                     (setq head0 (org-match-string-no-properties 4)))
            (setq head (org-link-display-format head0)
                  m (org-imenu-new-marker))
            (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
            (push (cons head m) (aref subs 1))))))
    (aref subs 1)))

(advice-add #'org-imenu-get-tree :override #'org-imenu-get-heading)

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.