Last Updated: February 25, 2016
· david

Emacs: Semi-automatic alignment of symbol: value pairs in Ruby and CoffeeScript

I like to keep my symbol value pairs aligned, like this:

# CoffeeScript

  key1:     1
  otherKey: 'value'

To do that, I'd select the lines, then do C-u M-x align-regexp, use :\(\s-*\) as the regexp, hit <RET> a couple of times, then n. It turns out there is a much easier way. Add the following to your Emacs config:

 (lambda ()
      (regexp . ":\\(\\s-*\\)")
      (group . 1)
      (modes . '(ruby-mode coffee-mode))
      (repeat . nil)))))

Then, select the lines you want to align, and simply do M-x align (or even better, bind that to a key combination).

Emacs' alignment facilities are really powerful. To learn more, see