Elisp: URL Percent Decode/Encode

By Xah Lee. Date: . Last updated: .

This page shows you how to percent decode/encode URI.

Examples of percent encoded URL:

Examples of decoded URL:

Decode URL

Here's solution.

(defun xah-html-decode-percent-encoded-url ()
  "Decode percent encoded URI of URI under cursor or selection.

Example:
    http://en.wikipedia.org/wiki/Saint_Jerome_in_His_Study_%28D%C3%BCrer%29
becomes
    http://en.wikipedia.org/wiki/Saint_Jerome_in_His_Study_(Dürer)

Example:
    http://zh.wikipedia.org/wiki/%E6%96%87%E6%9C%AC%E7%BC%96%E8%BE%91%E5%99%A8
becomes
    http://zh.wikipedia.org/wiki/文本编辑器

For string version, see `xah-html-url-percent-decode-string'.
To encode, see `xah-html-encode-percent-encoded-url'.
URL `http://ergoemacs.org/emacs/elisp_decode_uri_percent_encoding.html'
Version 2015-09-14."
  (interactive)
  (let ($boundaries $p1 $p2 $input-str)
    (if (use-region-p)
        (progn
          (setq $p1 (region-beginning))
          (setq $p2 (region-end)))
      (progn
        (setq $boundaries (bounds-of-thing-at-point 'url))
        (setq $p1 (car $boundaries))
        (setq $p2 (cdr $boundaries))))
    (setq $input-str (buffer-substring-no-properties $p1 $p2))
    (require 'url-util)
    (delete-region $p1 $p2)
    (insert (decode-coding-string (url-unhex-string $input-str) 'utf-8))))

thanks nns for the solution.

Encode URL

To encode, you can just use this:

(require 'url-util)
 (url-encode-url $input-str)

Here's a command version:

(defun xah-html-encode-percent-encoded-url ()
  "Percent encode URL under cursor or selection.

Example:
    http://en.wikipedia.org/wiki/Saint_Jerome_in_His_Study_(Dürer)
becomes
    http://en.wikipedia.org/wiki/Saint_Jerome_in_His_Study_(D%C3%BCrer)

Example:
    http://zh.wikipedia.org/wiki/文本编辑器
becomes
    http://zh.wikipedia.org/wiki/%E6%96%87%E6%9C%AC%E7%BC%96%E8%BE%91%E5%99%A8

URL `http://ergoemacs.org/emacs/elisp_decode_uri_percent_encoding.html'
Version 2015-09-14."
  (interactive)
  (let ($boundaries $p1 $p2 $input-str)
    (if (use-region-p)
        (progn
          (setq $p1 (region-beginning))
          (setq $p2 (region-end)))
      (progn
        (setq $boundaries (bounds-of-thing-at-point 'url))
        (setq $p1 (car $boundaries))
        (setq $p2 (cdr $boundaries))))
    (setq $input-str (buffer-substring-no-properties $p1 $p2))
    (require 'url-util)
    (delete-region $p1 $p2)
    (insert (url-encode-url $input-str))))

Emacs Lisp URL Percent Encode/Decode Functions

Emacs has a bundled library url-util for handling URL.

The main functions for manipulating URL are:

and decode-coding-string from emacs lisp itself.

Here's examples of using them.

url-hexify-string will turn a string into URL percent encoded string, including slash or colon characters used in URL such as http://.

(require 'url-util)

(url-hexify-string "'(),/\"" )
;; "%27%28%29%2C%2F%22"

(url-hexify-string "文本编辑器") ; Chinese
;; "%E6%96%87%E6%9C%AC%E7%BC%96%E8%BE%91%E5%99%A8"

(url-hexify-string "http://en.wikipedia.org/wiki/Saint_Jerome_in_His_Study_(Dürer)" )
;; "http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FSaint_Jerome_in_His_Study_%28D%C3%BCrer%29"

To decode a hexified string, use url-unhex-string then decode-coding-string. Example:

(require 'url-util)

(url-hexify-string "http://en.wikipedia.org/wiki/Saint_Jerome_in_His_Study_(Dürer)" )
;; "http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FSaint_Jerome_in_His_Study_%28D%C3%BCrer%29"

(decode-coding-string
 (url-unhex-string
  "http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FSaint_Jerome_in_His_Study_%28D%C3%BCrer%29")
 'utf-8)
;; "http://en.wikipedia.org/wiki/Saint_Jerome_in_His_Study_(Dürer)"

(url-hexify-string "文本编辑器")
;; "%E6%96%87%E6%9C%AC%E7%BC%96%E8%BE%91%E5%99%A8"

(decode-coding-string
 (url-unhex-string "%E6%96%87%E6%9C%AC%E7%BC%96%E8%BE%91%E5%99%A8" ) 'utf-8)
;; "文本编辑器"

To encode a full URL, use url-unhex-string then decode-coding-string. Example:

(require 'url-util)

(url-encode-url "http://zh.wikipedia.org/wiki/文本编辑器")
;; "http://zh.wikipedia.org/wiki/%E6%96%87%E6%9C%AC%E7%BC%96%E8%BE%91%E5%99%A8"

(url-encode-url "http://en.wikipedia.org/wiki/Saint_Jerome_in_His_Study_(Dürer)")
;; "http://en.wikipedia.org/wiki/Saint_Jerome_in_His_Study_(D%C3%BCrer)"

Elisp + JavaScript Solution

Here's a solution by calling a external node.js script. Elisp: Calling External Command to Decode URL Percent Encoding.

Liket it? Put $1 at patreon. Or Buy Xah Emacs Tutorial. Thanks.