Insert link: use selection as title if region active

Here is a small snippet that I find useful - when inserting an org-roam link:

  1. if region is active, use the selection as default suggestion to a link
  2. if region is not active, just do the usual org-roam-insert

This way you can quickly enrich an existing text with links.

I bind it to key instead of org-roam-insert.

(defun org-roam-insert-dwim ()
"Insert an org-roam link. If there is a region active, use it as name."
(interactive)
(push (if (region-active-p)
          (capitalize-dwim '(buffer-substring-no-properties
           (region-beginning)
           (region-end)))
        (thing-at-point 'symbol))
      regexp-history)
(call-interactively 'org-roam-insert))
2 Likes

Isn’t that a vanilla behaviour?

2 Likes

Ah… I’ve just checked and it is… I’ve been using this before org-roam had the capability and did not notice that it is default by now…