How do I remove an existing hyperlink without having to wade in and manually delete a bunch of brackets? When I try to org-insert-link and just delete the existing link, I get Lisp error: (error "Empty link").

I want to remove the link and preserve the text (i.e. the description).

up vote 7 down vote accepted

The following elisp function will take a link around the current point as recognised by org-bracket-link-regexp, so either [[Link][Description]] or [[Link]], and replace it by Description in the first case or Link in the second case.

(defun afs/org-replace-link-by-link-description ()
    "Replace an org link by its description or if empty its address"
  (interactive)
  (if (org-in-regexp org-bracket-link-regexp 1)
      (let ((remove (list (match-beginning 0) (match-end 0)))
        (description (if (match-end 3) 
                 (org-match-string-no-properties 3)
                 (org-match-string-no-properties 1))))
    (apply 'delete-region remove)
    (insert description))))
  • That's a very neat compact solution! I updated my answer to support [[LINK]] format org links too. I learned about match-beginning and match-end from your answer. – Kaushal Modi Apr 16 '15 at 14:09

I tried to add this to the answer from @Andrew, but it was too long for a comment...

I really liked his solution, except that it moved the cursor. (Technically I guess it moved the point. Anyway...) Fortunately, it was easy to add save-excursion to avoid that:

(defun afs/org-replace-link-by-link-description ()
  "Replace an org link by its description or if empty its address"
  (interactive)
  (if (org-in-regexp org-bracket-link-regexp 1)
      (save-excursion
        (let ((remove (list (match-beginning 0) (match-end 0)))
              (description (if (match-end 3) 
                               (org-match-string-no-properties 3)
                             (org-match-string-no-properties 1))))
          (apply 'delete-region remove)
          (insert description)))))

Call this command when the point is anywhere after the first [[ brackets of an org-link (or anywhere on/after an hyper-linked org-link).

An org link will be deleted if it is of the format [[LINK][DESCRIPTION]] or [[LINK]] in an org-mode buffer; else nothing will happen.

For safety, the discarded LINK from org-link is saved to the kill-ring in the event a need arises to use that link elsewhere.

(defun my/org-delete-link ()
  "Replace an org link of the format [[LINK][DESCRIPTION]] with DESCRIPTION.
If the link is of the format [[LINK]], delete the whole org link.

In both the cases, save the LINK to the kill-ring.

Execute this command while the point is on or after the hyper-linked org link."
  (interactive)
  (when (derived-mode-p 'org-mode)
    (let ((search-invisible t) start end)
      (save-excursion
        (when (re-search-backward "\\[\\[" nil :noerror)
          (when (re-search-forward "\\[\\[\\(.*?\\)\\(\\]\\[.*?\\)*\\]\\]" nil :noerror)
            (setq start (match-beginning 0))
            (setq end   (match-end 0))
            (kill-new (match-string-no-properties 1)) ; Save the link to kill-ring
            (replace-regexp "\\[\\[.*?\\(\\]\\[\\(.*?\\)\\)*\\]\\]" "\\2" nil start end)))))))

The quickest may be to place the cursor before the link, then type C-M-space (mark-sexp), which will mark the entire link. Then delete it by typing a backspace (if you use delete-selection-mode) or C-w.

  • 1
    This seems to delete the entire link, but I want to remove the link and preserve the text (i.e. the description). – incandescentman Apr 16 '15 at 1:15
  • 1
    Sorry, I guess I misunderstood your question. – Harald Hanche-Olsen Apr 16 '15 at 8:01
  • 1
    To the downvoters out there: The question was ambiguous when I answered it. It has been edited since then, removing the ambiguity. I do leave the answer up, though, since it still seems the easiest way to delete the entire link, description and all. – Harald Hanche-Olsen Apr 17 '15 at 7:03

This very quick and very dirty macro would be one way, not the best way:

(fset 'my/org-remove-link
   [?\M-a delete delete ?\M-x ?z ?a ?p ?- ?t ?o ?- ?c ?h ?a ?r return ?\[ ?\C-e backspace backspace ?\C-x])

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.