Emacs: URL to Link Command

By Xah Lee. Date: . Last updated: .

Here's a command that wraps a anchor tag (link) at the cursor position.

For example, you have under cursor:


Press a button, and it's changed to

<a href="http://en.wikipedia.org/wiki/Emacs">Emacs</a>

Put this in your emacs init file:

(defun xah-html-url-linkify ()
  "Make the URL at cursor point into a HTML link.
Work on current non-whitespace char sequence or text selection.

URL `http://ergoemacs.org/emacs/wrap-url.html'

Version 2018-03-22"
  (let ( $p1 $p2 $new-str )
    (if (region-active-p)
        (progn (setq $p1 (region-beginning) $p2 (region-end)))
        (skip-chars-backward "^ \n\t")
        (setq $p1 (point))
        (skip-chars-forward "^ \n\t" )
        (setq $p2 (point))))
    (setq $new-str (file-relative-name
                    (replace-regexp-in-string "^file:///" "/" (buffer-substring-no-properties $p1 $p2) t t)))
    (delete-region $p1 $p2)
    (insert (concat "<a href=\"" (url-encode-url $new-str) "\">" $new-str "</a>" ))))

Emacs HTML Topic

  1. Emacs HTML Tips
  2. Emacs: Xah HTML Mode
  3. Emacs: Insert HTML Tag
  4. Emacs: URL to Link Command
  5. Elisp: Replace HTML Entities
  6. Emacs: CSS Compressor
  7. Emacs: Working with CSS Color Values
  8. Emacs: View Image Thumbnails
  9. Emacs: HTML Image Path to Img Tag
  10. Emacs: Convert Image Files in Dired
Patreon me $5 patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboard for Emacs

Patreon me $5. Ask me question on patreon