Emacs: Key Macro Example: Insert All Unicode Bullets
This page shows a example of using Emacs: Key Macro to insert all Unicode characters whose name contains “bullet”.
Problem
Find all Unicode characters whose name contains “bullet”. For each one, show the Unicode name, followed by the character. For example, here's the result showing some of them:
BULLET • TRIANGULAR BULLET ‣ WHITE BULLET ◦ CIRCLED BULLET ⦿
Before reading further, try to spend 10 minutes, using your knowledge of emacs to do it. Test your skill out.
Hint: use ucs-insert
and emacs keyboard macro.
(note: ucs-insert
is obsolete since emacs 24.3.
〔see Emacs Version History〕
Use insert-char
instead.)
If you stuck somewhere, see:
Solution
Here's one way to do it.
- Alt+x
insert-char
. - Type
*bullet*
then Tab twice, to get a completion list of all names containing “bullet”. - Move cursor to the completion pane, copy all content, put it in a new file, delete the first few irrelevant lines. You should get this:
BLACK LEFTWARDS BULLET BLACK RIGHTWARDS BULLET BULLET BULLET OPERATOR CIRCLED BULLET CIRCLED WHITE BULLET HYPHEN BULLET INVERSE BULLET REVERSED ROTATED FLORAL HEART BULLET ROTATED FLORAL HEART BULLET ROTATED HEAVY BLACK HEART BULLET TRIANGULAR BULLET WHITE BULLET
- Now, turn on
whitespace-mode
, so you can see the tabs or spaces. 〔see Emacs: Show Whitespaces〕 - Now, do find/replace, and replace sequence of more than 2 white spaces to Newline. 〔see Emacs: Find Replace in Current File〕 You should have this:
BLACK LEFTWARDS BULLET BLACK RIGHTWARDS BULLET BULLET BULLET OPERATOR CIRCLED BULLET CIRCLED WHITE BULLET HYPHEN BULLET INVERSE BULLET REVERSED ROTATED FLORAL HEART BULLET ROTATED FLORAL HEART BULLET ROTATED HEAVY BLACK HEART BULLET TRIANGULAR BULLET WHITE BULLET
- Optional: call
delete-trailing-whitespace
to get rid of trailing whitespaces, if any. - Now start Emacs Keyboard Macro. Starting at the first line. You want to select the whole line, copy it, then Alt+x
insert-char
, paste it, press Enter to insert the Unicode char of that name. Then, move cursor to next line. Repeat. - In the end, you'll have this:
- BLACK LEFTWARDS BULLET ⁌
- BLACK RIGHTWARDS BULLET ⁍
- BULLET •
- BULLET OPERATOR ∙
- CIRCLED BULLET ⦿
- CIRCLED WHITE BULLET ⦾
- HIGH-SPEED TRAIN WITH BULLET NOSE 🚅
- HYPHEN BULLET ⁃
- INVERSE BULLET ◘
- REVERSED ROTATED FLORAL HEART BULLET ☙
- ROTATED FLORAL HEART BULLET ❧
- ROTATED HEAVY BLACK HEART BULLET ❥
- TRIANGULAR BULLET ‣
- WHITE BULLET ◦
All this sounds complicated, but if you use emacs daily, it usually is done with reflex and can be finished within 2 minutes.
Now, try a little exercise. Try to list all Unicode chars whose name contains “star” in it. See what kinda starry chars Unicode has. 〔see Unicode Stars ★ ✪ ✭ ✡ ⛤ ❉ ❄ ✿ 🌟 🌠〕
Emacs Lisp Solution
;; print all Unicode chars up to 2^16 whose name contains "BULLET" (mapc (lambda (x) (let ((nn (get-char-code-property x 'name))) (when (and (not (null nn)) (string-match "BULLET" nn)) (insert-char x) (insert " " nn "\n") ) ) ) (number-sequence 0 (expt 2 16))) ;; • BULLET ;; ‣ TRIANGULAR BULLET ;; ⁃ HYPHEN BULLET ;; ⁌ BLACK LEFTWARDS BULLET ;; ⁍ BLACK RIGHTWARDS BULLET ;; ∙ BULLET OPERATOR ;; ◘ INVERSE BULLET ;; ◦ WHITE BULLET ;; ☙ REVERSED ROTATED FLORAL HEART BULLET ;; ❥ ROTATED HEAVY BLACK HEART BULLET ;; ❧ ROTATED FLORAL HEART BULLET ;; ⦾ CIRCLED WHITE BULLET ;; ⦿ CIRCLED BULLET
(2014-04-16 thanks to Martin for initial solution)
Character Properties (ELISP Manual)