Elisp: Character Type

By Xah Lee. Date: . Last updated: .

What is Char Type

In emacs lisp, characters are represented as integers of the character's Unicode: Code Point (Char ID)

For example, the char “a” in elisp is just 97, because its codepoint is 97.

No Dedicated Char Type

There is no actual character type in emacs lisp. Whether a integer is a character depends on programer's intention.

Readable Char Syntax

You can also represent char by (string-to-char "a")

(eq 97 ?a )
;; t

(eq 97 (string-to-char "a"))
;; t

ASCII Control Chars and Backslash

Syntax of the form ?\char may have special meaning, depending what char is. They either represent a ASCII control character, or just the character char. For example, ?\n is the newline char. ?\\ is backslash char.

Here's a list of special meaning with the backslash:

syntaxcodepointnameinput
?\a7bellC-g
?\b8backspaceC-h
?\t9horizontalC-i
?\n10line feedC-j
?\v11vertical tabC-k
?\f12formfeedC-l
?\r13carriage returnC-m
?\e27escapeC-[
?\s32spaceSPC
?\\92backslash\
?\d127delete characterDEL

〔see ASCII Characters

some other important special case

Find a Char's Codepoint Interactively

call describe-char

Insert Char

insert-char

(insert-char CHARACTER &optional COUNT INHERIT)

insert character by its Unicode: Code Point (Char ID).

(insert-char 97 3)
;; aaa

Get Char from Buffer

char-before

return the codepoint of character before cursor.

char-after

return the codepoint of character after cursor.

Convert Char and String

char-to-string

(char-to-string CHAR)

convert a CHAR (codepoint) to string of single character.

string-to-char

(string-to-char STRING)

return the first char in string. (return a integer that's the char's codepoint)

string

(string &rest CHARACTERS)

convert chars to string.

(string 97)
;; "a"

(string 97 98)
"ab"

Equality Test, Check is Char or String

char-equal

(char-equal C1 C2).

Return t if two characters match, but dependent on a Buffer Local Variable case-fold-search.

Case is ignored if case-fold-search is non-nil in the current buffer.

🛑 WARNING: when at beginning of buffer, char-before return nil. Using char-equal result error because argument needs to be integer. So, in general, better use eq if you don't care about letter case. 〔see Elisp: Equality Test

;; check if char before is a newline
(char-equal (char-before) 10)
;; depends on case-fold-search for letter characters

;; better use eq for single case characters, because char-before return nil if point is at beginning, and char-equal would error because it expect integer
(eq (char-before) 10)
char-or-string-p

check if object is char or string.

Replace Chars

Reference

Elisp, String

Elisp, character and syntax table