Elisp: Unicode Escape Sequence

By Xah Lee. Date: . Last updated: .

In emacs lisp string, you can have Unicode characters directly (For example, "I β™₯ 😸"), or, you can represent Unicode char by the following syntax:

"\uxxxx"
A Unicode char. xxxx must be 4 hexadecimal digits, representing the char's codepoint in hex. You need to pad it with 0 if the codepoint is less than 4 hexadecimal digits.
"\U00xxxxxx"
A Unicode char. xxxxxx must be 6 hexadecimal digits, representing the char's codepoint in hex. You need to pad it with 0 if the codepoint is less than 6 hexadecimal digits.

Note: the syntax is a bit ugly. Which one to use depends on whether the Unicode is in the range of 0 to 4 hexadecimal digits. (Each Unicode char is given a integer id, called its β€œcodepoint”. [see Unicode Basics: Character Set, Encoding, UTF-8, Codepoint])

;; examples of Unicode char representation in string

;; lower case β€œa”
(search-forward "\u0061" )

;; β™₯ BLACK HEART SUIT codepoint 9829, #x2665
(search-forward "\u2665" )

;; 😸 GRINNING CAT FACE WITH SMILING EYES codepoint 128568, #x1f638
(search-forward "\U0001f638" )

;; β™₯ 😸

in the above example, the letter a's Unicode hexadecimal is just β€œ61”, so you need to pad it with β€œ00”.

in the above example, the grinning cat 😸's codepoint in hexadecimal is 5 digits. So, you need to use the "\U00xxxxxx" form, and because it's less than 6 digits, so you need to pad it with β€œ0”, resulting β€œ000” there.

Note: you can find a Unicode char's codepoint by Alt+x describe-char.

[see Emacs: Unicode Tutorial]

Why is Encoded Unicode Char Useful?

The use of encoded representation is useful when you want to represent non-printable chars, such as {RIGHT-TO-LEFT MARK, ZERO WIDTH NO-BREAK SPACE, NO-BREAK SPACE}. Example:

(defun replace-BOM-mark-etc ()
  "Query replace some invisible Unicode chars.
The chars to be searched are:
 RIGHT-TO-LEFT MARK 8207 x200f
 ZERO WIDTH NO-BREAK SPACE 65279 xfeff

start on cursor position to end."
  (interactive)
  (query-replace-regexp "\u200f\\|\ufeff" ""))

(info "(elisp) General Escape Syntax")

Emacs Lisp Advanced

Lisp Data Structure

Lisp Symbol

Elisp Misc