5

From the documentation of insert-char, I cannot see why

(insert-char "GREEK SMALL LETTER EPSILON")

doesn't work. Is there a non-interactive way to insert a character given its Unicode name?

8

From the documentation of insert-char, I cannot see why

(insert-char "GREEK SMALL LETTER EPSILON")

doesn't work.

It doesn't work because insert-char understands Unicode character names only when called interactively (e.g. via C-x8RET or M-xinsert-charRET), as stated in its docstring:

Interactively, prompt for CHARACTER.  You can specify CHARACTER in one
of these ways:

 - As its Unicode character name, e.g. "LATIN SMALL LETTER A".
   Completion is available; if you type a substring of the name
   preceded by an asterisk ‘*’, Emacs shows all names which include
   that substring, not necessarily at the beginning of the name.

 - As a hexadecimal code point, e.g. 263A.  Note that code points in
   Emacs are equivalent to Unicode up to 10FFFF (which is the limit of
   the Unicode code space).

 - As a code point with a radix specified with #, e.g. #o21430
   (octal), #x2318 (hex), or #10r8984 (decimal).

The interactive spec of insert-char delegates to read-char-by-name for Unicode name completion and translation to the corresponding character code.

When insert-char is called from Lisp, its first argument CHARACTER should satisfy the predicate characterp.

Is there a non-interactive way to insert a character given its Unicode name?

There are multiple ways; here's one for Emacs 26 and subsequent versions:

(insert (char-from-name "GREEK SMALL LETTER EPSILON"))

Here's another for earlier Emacs versions:

(insert (cdr (assoc "GREEK SMALL LETTER EPSILON" (ucs-names))))
  • Where is char-from-name defined? – JeanPierre Jul 4 '18 at 9:49
  • @JeanPierre M-x find-function RET char-from-name RET, which reminds me that char-from-name was just added in Emacs 26; I've updated the answer accordingly. – Basil Jul 4 '18 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.