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])

;; 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")

Put $5 at https://www.patreon.com/xahlee , or goto paypal.com and pay to Xah@XahLee.org . Message me on XahLee discord, say so, ask me questions.
Or Buy Xah Emacs Tutorial

Emacs Tutorial

Emacs Init

Emacs Keys

ELisp

ELisp Examples

ELisp Write Major Mode


ELisp

Basics

Basic Functions

Writing Command

Writing Script

Lisp Data Structure

Lisp Symbol

Elisp Misc