Using Unicode in Emacs for Perl 6

I use vi/vim for quick edits and remote work, but I do most of my programming (and other work) in emacs. To enter Unicode characters in emacs, you run the "insert-char" command, which by default is tied to "C-x 8 [Enter]", then type in the hex code for the character or its name.

Typing at least 5 characters to get one got old very fast, now that there are some Unicode characters that can be used as Perl 6 operators. So I wrote a lisp function which asks for a single character and looks it up in an alist (kinda similar to a Perl hash). That way I can enter any Unicode character I've put in the alist by hitting two keys: one key to run the function, then whatever key I assigned to that character. Here's the lisp, which I put in my .emacs file:

(defun ajb/common-unicode-chars (ch)
  "Function to map single keys to some common unicode characters.
Run the function and then press a key.  Non-mapped keys will default to themselves."
  (interactive "cChar: ")
   (or (cdr
         (char-to-string ch)
         '(("[" . ?«)
           ("]" . ?»)
           ("a" . ?Α)
           ("d" . ?Δ)
           ("g" . ?Γ)
           ("o" . ?Ω)
           ("p" . ?π)
           (">" . ?⊃)
           ("<" . ?⊂)
           ("|" . ?∪)
           ("&" . ?∩)
           ("1" . ?⚀)
           ("6" . ?⚅)))) ch)))
(global-set-key (kbd "<f6>") 'ajb/common-unicode-chars)

The first two characters in the list are guillemets, which Perl 6 uses as the "hyperoperators" (more on those another time), and they can also be used as double-quotes for strings. I keyed them to the square brackets, since they don't require the shift key. Then I put in a few Greek letters I might want to use, a few of the Set operators, and a couple of dice faces, just to show some possibilities. As I discover more Unicode characters that I want to use more than once in a great while, I'll pick a key for them and add them to the list.

If you put this in your .emacs, it will be bound to the <f6> key in all modes. So to get the left-pointing guillemet, you'd hit "<f6> [". If you know lisp, the code is pretty simple. If you don't, I think it's fairly obvious how to add more entries, but please leave a comment if you have any questions.

Thanks to all the Unicode characters that are available, along with some other new features like "pointy blocks," you can write Perl 6 code that looks pretty strange to a Perl 5 guy, like this:

#!/usr/bin/env perl6
use v6;

my &Α = -> {'a'..'n');

my &Ω = -> {'m'..'z');

say [∩] Α, Ω;

I'll save explaining what that does for next time.


If you use X, there is a better solution: the Compose key. Since it is a basic X feature, it works in any place where you type text, not just Emacs. You get a huge number of predefined shortcuts, so you do not need to write a configuration yourself. The shortcuts are also more mnemonic. See for instructions. Additional operators added from

Compose, <, < → «
Compose, >, > → »
Compose, *, A → Α
Compose, *, D → Δ
Compose, *, G → Γ
Compose, *, V → Ω
Compose, *, p → π
Compose, {, ) → ⊃
Compose, {, ( → ⊂
Compose, {, U → ∪
Compose, {, ^ → ∩
Compose, [, 1, ] → ⚀
Compose, [, 6, ] → ⚅
Compose, i, n → ∈
Compose, !, i, n → ∉
Compose, n, i → ∋
Compose, !, n, i → ∌
Compose, !, {, ( → ⊄
Compose, !, {, ) → ⊅
Compose, {, =, ( → ⊆
Compose, {, =, ) → ⊇
Compose, (, -, ) → ⊖

Leave a comment

About Aaron Baugher

user-pic I'm a programmer and Unix sysadmin who uses Perl as much as possible, operating from the Midwest USA. To hire me for sysadmin or programming work, contact me at aaron.baugher @ or as 'abaugher' on #perl6.