reddit / emacs

What method do you use to create internal links in org?

JDRiverRun · 2022-11-01 23:03
I was always surprised that creating an internal link to another heading in org-mode is so laborious: navigate to that heading, store the link, navigate back, insert it. consult-org-heading is a really slick interface to quickly narrow the headings list and jump to one. So why not re-use it to directly insert *links to headings* at point? I threw together [this small gist](https://gist.github.com/jdtsmith/8602d998116b953725218224b77b8766) which works well for this. Now I can invoke consult-org-heading, use orderless to winnow to the heading I want, `C-,` to embark, and `l` to insert the link. What methods do you use for quick internal link insertion in org?
iwaka · 2022-11-02 00:06
I use a similar method. [This function](https://github.com/goderich/dotfiles/blob/54690dd0cd16fb2e47954b9933e5bb20905f183f/doom-emacs/.config/doom/functions.el#L297) allows me to filter org headlines in the current buffer, and prompts me for a link name (if none is provided, the link name defaults to the heading text). I also have functions to link to a heading with a custom ID, or a unique ID, in case I ever change or move the heading. You can see these a little below the linked function. These currently use ivy, though I'm in the middle of migrating to consult. Any tips on how to rewrite these functions would be appreciated (couldn't find a simple way to get the text of the heading with consult). **Edit:** just migrated org linking functionality to consult. New version [here](https://github.com/goderich/dotfiles/blob/master/doom-emacs/.config/doom/functions.el#L299).
trembel12 · 2022-11-02 04:57
I use org-roam for all my org-files, where I can use their system to link to other org-roam notes/headings
podtatranec · 2022-11-02 05:40
Yup, org-roam-node-insert
NoisyFrequency · 2022-11-02 12:13
I think I found a solution which doesn't depend on any other package, [thanks to this SO](https://stackoverflow.com/questions/70359184/get-list-of-all-h1-entry-of-org-mode-buffer-or-file), It only works within a file and not for linking between different files but in short, it goes through the tree of all headings and collects them, then you can select the heading you want with `completing-read` and link to it. (defun nf/parse-headline (x) (plist-get (cadr x) :raw-value)) (defun nf/get-headlines () (org-element-map (org-element-parse-buffer) 'headline #'nf/parse-headline)) (defun nf/link-to-headline () "Insert an internal link to a headline." (interactive) (let* ((headlines (nf/get-headlines)) (choice (completing-read "Headings: " headlines nil t)) (desc (read-string "Description: " choice))) (org-insert-link buffer-file-name (concat "*" choice) desc))) It looks [like this](https://imgur.com/a/GYZqDF6).
JDRiverRun · 2022-11-02 12:35
Nice. Does this create :ID: properties on the target if org is configured to do so?
JDRiverRun · 2022-11-02 12:41
Nice. Consult-org-heading is similar, but color codes the heading hierarchy and can operate across files. One advantage of org-{store,insert}-link is that they’ll auto-create :ID: on the target if that’s how you’ve configured it (org-id-link-to-org-use-id).
publicvoit · 2022-11-02 13:16
I'm going to create a short demo for the EmacsConf22 based on https://karl-voit.at/2020/07/22/org-super-links/ https://karl-voit.at/2019/11/16/UOMF-Linking-Headings/ https://karl-voit.at/2021/01/23/org-linker-edna/
iwaka · 2022-11-02 14:38
Yes! [This function](https://github.com/goderich/dotfiles/blob/aba64bdfde7b825e3dd495bae86678f70260bf7c/doom-emacs/.config/doom/functions.el#L320) creates a link with an ID. The actual retrieval of the ID is in the helper function right above. If no ID is present, it is created automatically.
wilbur-d · 2022-11-02 14:43
[org-super-links](https://github.com/toshism/org-super-links) handles this pretty well. If you are more interested in building something custom, I also created [org-linker](https://github.com/toshism/org-linker) to serve as a bit of a standard base to help with these types of things.