Emacs Keyboard Macro Example: Insert All Unicode Stars

Perm url with updates: http://xahlee.org/emacs/emacs_macro_insert_all_unicode_stars.html

Emacs Keyboard Macro Example: Insert All Unicode Stars

Xah Lee, 2010-12-24

Here's another story about emacs's power. This page shows a real world example of emacs's power, using several of emacs's features to insert all unicode characters who's name contains “star”.

Problem

I need to collect all unicode characters whose name contains “bullet”. For each one, i want the unicode name, followed by the char. For example, here's the result showing some 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.

  • If you don't know how do it, you are a emacs learner.
  • If you did it within 10 min, you are a emacs user.
  • If you did it within 1 min, you are a emacs expert.
  • If you did it within 1 min without emacs, you are impossible. ☺

Solution

Hint, use “ucs-insert” and keyboard macro . If you stuck somewhere, it might help to refresh Emacs and Unicode Tips. You should be using emacs 23.x.

Here's how i did it.

  • Call “ucs-insert”.
  • 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
  • Optional: now, turn on whitespace-mode, so you can see the tabs or spaces.
  • Now, do find and replace, and replace sequence of more than 2 white spaces to Newline. 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 call “ucs-insert”, 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
TRIANGULAR BULLET
WHITE 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

All this sounds complicated, but if you use emacs daily, it usually is done with reflex and can be finished within 30 seconds.

Now, try a little exercise. Try to list all unicode chars whose name contains “star” in it. See what kinda starry chars unicode has. (you should get 45 chars. Counting only unique ones, it should be 42.)

Was this page useful? If so, please do donate $3, thank you donors!

Popular posts from this blog

11 Years of Writing About Emacs

does md5 creates more randomness?

Google Code shutting down, future of ErgoEmacs