Using prettify-symbols in Clojure and Elisp without breaking indentation09 Nov 2015, by Artur Malabarba.
prettify-symbols-mode is a very nice minor-mode that is a little too modest
for its own good. You can turn it on right now if you’re using a recent Emacs,
but it’ll do nothing more than turn
Still, it’s powerful and versatile and deserves that you give it a try. To
extend its feature-set you can install packages or customize it yourself, and
that’s what we’re here to do today, specifically in
First of all, let’s make sure it’s turned on.
If you read the docstring for this mode, it’ll explain that any symbol can be
displayed as any character, so the first thing that comes to my mind is
≥. But that comes with a drawback. Suddenly
that symbol is 1 character shorter, so Emacs is going to indent sexps
accordingly, and people reading your code will see bad indentation.
The solution is to configure
prettify-symbols-mode to compose these symbols in
a special way. This feature is somewhat accidental, and wasn’t even documented
in the last release. The composition rules are slightly complicated to write,
and for that I’ll point to the docstrings of
compose-region (see the third
reference-point-alist. Below are several examples you can toy
One way to fix the width, is to join two spaces together, and then stick the inequality on top of them.
The option I prefer is to just add a small dot before the symbol. This makes it
clear that the symbol occupies the space of two characters, while still looking
nicer than a plain
Then there are the
->> macros, which are in dire need of a makeover
if you ask me. The solution I currently use is a spaced-out version of
(you could also use
If you don’t like that, there’s also the option of adding one or two dashes inside the symbols to make our fake arrow prettier.
And finally, none of this will work if we don’t set it up. Note that
clojure-mode already defines
fn to display as
λ, so we don’t need to
configure this one.