Backlinks

Backlinks

It can be useful to search for all notes that link to the current note. You can use org-occur-link-in-agenda-files to search for backlinks to the current node.

Unfortunately, out of the box this requires that the link description exactly match the title of the node. Here is an alternative solution that searchs for any backlink, no matter the description.

(defun zorg-backlinks ()
  "Search for backlinks to current entry."
  (interactive)
  (let ((link (condition-case nil
                  (org-id-store-link)
                (error "Unable to create a link to here"))))
    (org-occur-in-agenda-files (regexp-quote link))))

If you use org-ql you might prefer this:

(defun zorg-backlinks ()
  (interactive)
  (let* ((link (condition-case nil
                  (org-id-store-link)
               (error "Unable to create a link to here")))
         (query `(link :target ,link)))
    (org-ql-search
      org-agenda-text-search-extra-files
      query
      :title (concat "Links to: " (org-get-heading t t)))))

If you use org-ql and you want to maintain a dynamic block of backlinks, you can use this:

(defun org-dblock-write:zorg-backlinks (&rest params)
  (let ((elements (org-ql-query
                    :from org-agenda-text-search-extra-files
                    :where `(link :target ,(org-id-store-link)))))
    (insert "Backlinks:\n")
    (dolist (element elements)
      (insert "- " (org-make-link-string (concat "id:" (org-element-property :ID element))
                                         (org-element-property :raw-value element))
              "\n"))
    (delete-char -1)))

You can then add dynamic blocks:

Backlinks:

These can be updated using C-c C-c on the block. For other options to keep the blocks up to date, see the documentation.

Created: 2020-11-28 Sat 18:05

Validate