mmr_hw
Last Updated: February 25, 2016
·
2.191K
· 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

root:
  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:

(add-hook
 'align-load-hook
 (lambda ()
   (add-to-list
    'align-rules-list
    '(symbol-value-alignment
      (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 http://emacswiki.org/emacs/AlignCommands