I've been trying to hide the URL component of the markdown links in my buffer.

I could select the markdown links using


However, I just want to capture the second half (the URL component) of the links and fade them away in the buffer.

I have tried every possible documentation available but with no avail. Please help.

  • You should add ? to your reqular expressions to avoid greedy matching, i.e., "\(\[.+?\]\)\((.+?)\)".
    – Tobias
    Commented Nov 7, 2016 at 11:22
  • To capture the second half of the re just use the right subexpressions, such as (match-beginning 1) and (match-end 1).
    – Tobias
    Commented Nov 7, 2016 at 11:24
  • I have tried that as well, could you please provide one example?
    – myTerminal
    Commented Nov 7, 2016 at 11:26
  • It does not seem to identify the sub-expressions, I tried \(\[.+?\]\)\((.+?)\)(match-end 1)
    – myTerminal
    Commented Nov 7, 2016 at 11:31

1 Answer 1


The following minimal example code shows how to hide the urls in md-files:

(defun md-hide-urls ()
  "Hide all urls in the current buffer."
  (goto-char (point-min))
  (while (re-search-forward "\\(\\[.+\\]\\)\\((.+)\\)" nil t)
    (put-text-property (match-beginning 2) (match-end 2) 'invisible t)))

Regular expression search via lisp is described in Section 33.4 of the emacs manual. How to make text invisible is described in Section 37.6 of the emacs manual. You find informations on accessing match strings in Section 37.6.2 of the emacs manual.

  • Thanks. I've been looking (highlight-regexp <reg-exp> <face>). I highly doubt I could use it there.
    – myTerminal
    Commented Nov 7, 2016 at 12:19
  • 1
    @myTerminal AFAIK there is no face having the same effect as the invisibility text property. See also: emacs.stackexchange.com/questions/13343/…
    – Tobias
    Commented Nov 7, 2016 at 15:55

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.