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-07-28"
  (let ( $p1 $p2 $input $newStr )
    (if (use-region-p)
        (setq $p1 (region-beginning) $p2 (region-end))
        (let ($p0)
          (setq $p0 (point))
          ;; chars that are likely to be delimiters of full path, e.g. space, tabs, brakets.
          (skip-chars-backward "^  \"\t\n'|[]{}<>〔〕“”〈〉《》【】〖〗«»‹›·。\\`")
          (setq $p1 (point))
          (goto-char $p0)
          (skip-chars-forward "^  \"\t\n'|[]{}<>〔〕“”〈〉《》【】〖〗«»‹›·。\\'")
          (setq $p2 (point)))))
    (setq $input (buffer-substring-no-properties $p1 $p2))
    (setq $newStr
          (if (string-match "^http" $input )
            (progn (file-relative-name (replace-regexp-in-string "^file:///" "/" $input t t)))))
    (delete-region $p1 $p2)
    (insert (concat "<a href=\"" (url-encode-url $newStr) "\">" $newStr "</a>" ))))

HTML Topic

  1. Emacs HTML Tips
  2. Insert HTML Tag
  3. URL to Link Command
  4. Lines to HTML Table
  5. Replace HTML Entities
  6. CSS Compressor
  7. Color CSS Color Values
  8. View Image Thumbnails
  9. HTML Image Path to Img Tag
  10. Convert Image Files in Dired
  11. Xah HTML Mode

Elisp HTML Commands

  1. wrap-url
  2. Make URL links
  3. Make Image Links
  4. Extract URL
  5. Word to Wikipedia Linkify
  6. Wikipedia URL Linkify
  7. URL Percent Decode/Encode
  8. Lines to HTML Table
  9. Markup Function Names
  10. Color Source Code
  11. Transform Text Under Cursor
  12. Chinese Char Reference Linkify
  13. HTML make-citation
  14. update-title
  15. Google Map Linkify 🌐
  16. CSS Compressor
  17. Make Ruby Annotation
  18. Move Image File

If you have a question, put $5 at patreon and message me.
Or Buy Xah Emacs Tutorial
Or buy a nice keyboard: Best Keyboards for Emacs


Emacs Lisp