2011-05-16

toward balanced and colorful delimiters

Emacs has the very useful feature of blinking the corresponding left "(" when you type its partner ")" (and the same for other delimiters like "[]", "{}" etc., depending on the mode). This is very useful for programming, especially for languages like Lisp and Scheme and the like.


Further help can come from tools like autopair or paredit – although the latter is a bit too much bondage & discipline for me, many people love it.
Anyway, recently I discovered a new helper in the quest for balance in delimited universe: rainbow-delimiters. With this package, the delimiters all get different colors based on their nesting level. It works wonderfully well.
Installation is straightforward:
  • download rainbow-delimiters and put it in your load-path
  • add something like the following in your .emacs:
(when (require 'rainbow-delimiters nil 'noerror) 
  (add-hook 'scheme-mode-hook 'rainbow-delimiters-mode))
That's it – of course you'll need to do the equivalent for modes where you'd like to enable it. See the screenshot below – maybe a bit too colorful for some people, but I like it and I have found it actually useful to see the corresponding delimiters without having to move the cursor over them.


I already added some nice Zenburn colors for this to the Zenburn theme for Emacs-24, as can be seen in the screenshot.

10 comments:

p4bl0 said...

That's pretty cool. What I would like to have also is a way to have Emacs highlight delimiters like corresponding open and close html tags or LaTeX begin and end of environment. (instead of highlighting < and > in html and { and } in LaTeX, which is good but not nearly as useful).

eeeickythump said...

Highlight-parentheses-mode does something similar, but applies it only to the pairs of parentheses that contain the point.

http://www.emacswiki.org/emacs/HighlightParentheses

Leigh Klotz said...

I remember in 1981 buying a plotter at Radio Shack with 8 colored pens and sending a note off to Scott Fahlman who had suggested color-balanced parens. Emacs (even Zwei) was monochrome then...

Jeremy Rayman said...

I'm the author of rainbow-delimiters and it's an honor to be featured on emacs-fu. I've been following the blog for years now.

You happened to blog about the mode a week before I put out a big update - adding new options to the customize interface, color themes for both light and dark backgrounds, infinite depth support, and the ability to turn off highlighting of any type of delimiter if you prefer not to see it highlighted in the language you're using.

My goal is to really solve the need for a mode like this, for any language. The first step was making it FAST - turning the mode on simply must not impact scrolling or editing speed. Now that that's done I'm adding customization tweaks to make it useful for a variety of languages.

XML/HTML tag highlighting is on the todo list. In the next release the mode will give the option to track and color each kind of delimiter independently, which fits better with C-like languages.

I've got some neat ideas for other language support, e.g. highlighting opening delimiters in Python according to their indentation. Re: p4bl0's comment, I'll add LaTeX tags to the todo list, but don't expect them anytime soon!

Despite the screenshot in the post, the mode's default color scheme is very subtle; the colors shouldn't be obvious until you're actually looking for a visual cue from them. I think with the understated colors, having rainbow-delimiters on actually makes the editor more beautiful - I once thought to myself it looked like little glints of color from pebbles in a stream ;)

Thanks for your wonderful pages of Emacs tips. I couldn't even count the number of things I've learned from your blog over the years.

djcb said...

@JLR: great to see you here! Thank you for creating rainbow-delimiters, it's a really nice piece of work!

I often try different modes, and many contain nice ideas, but often they also they're just not useful enough, have some side-effect etc., so after a while I turn those off.

Not so for rainbow-delimiters -- I turned them on, and they're there, they're helpful yet non-intrusive and fast!

Regarding the colors, I just tried the default ones again, and indeed they are subtle! I can see the thought that went into that.

So again, many thanks for creating rainbow-delimiters; it's a great idea with a very fine implementation; looking forward to the future version!!

Ruben Berenguel said...

Pretty neat indeed, and with the comment of the developer... I think I'll have to test it :) But there are so many things to do these days (like writing some more posts about that emacs 30 day challenge back in December... oh, how quickly time passes!!)

Cheers,

Ruben @mostlymaths.net

Anonymous said...

How do I turn it on permanently for all major modes? in all buffers?

I tried just with

(rainbow-delimiters-mode 1)

in my .emacs. It doesn't work when I start emacs, but if I evaluate .emacs it does. But only for that buffer.

Paw

Pablo said...

Perhaps it's been already answered before, but anybody knows the name of the font from the screenshot ? Sorry for this out-of-topic noob question.

djcb said...

@Anonymous: I don't think you can set it globally, but you have to do per-mode, using add-mode-hook, so e.g. something like:

(add-hook 'emacs-lisp-mode-hook 'rainbow-delimiters-mode)

in your .emacs should do the trick for all emacs-lisp buffer.

@Pablo: DejaVu Sans Mono-9

Anonymous said...

I installed rainbow-delimiters. As a result, all the parens and brackets turned gray. I'd been warned the coloring would be "subtle" and "understated", but it looks like the Emperor's new clothes.