Jakob Lnr
Jakob Lnr

Reputation: 145

How can I indent Ruby and Rails code in Vim?

I just wonder if it´s possible to auto indent Rails Code in Vim instead of this:

validates :email, :presence => true,
  :format => { :with => email_regex },
  :uniqueness => { :case_sensitive => false }

to this:

validates :email, :presence   => true,
                  :format     => { :with => email_regex },
                  :uniqueness => { :case_sensitive => false }

Upvotes: 6

Views: 2184

Answers (3)

Jakob Lnr
Jakob Lnr

Reputation: 145

Thanks for the answers.

If anyone needs this too, in Tabular this works with:

:Tabularize /^[^:]*\zs:/r1c0l0
:Tabularize /^[^=>]*\zs=>/l1

If you want to use this in a function in your vimrc:

function IndentV()
  Tabularize /^[^:]*\zs:/r1c0l0
  Tabularize /^[^=>]*\zs=>/l1
endfunction
map <Leader>iv :call IndentV()<cr>

So you just select text in visual mode an press \iv to make this happen.

Upvotes: 1

the Tin Man
the Tin Man

Reputation: 160551

I use both the Align and Tabular plugins.

Align has some nice pre-built alignments, whereas Tabular allows you to design a regex that will be used to match the fields. I find myself using Tabular more these days, but your mileage might vary.

Upvotes: 0

sleepynate
sleepynate

Reputation: 8036

The best way to do this is not actually to use built-in alignment in Vim, but rather the Align Plugin by Dr. Chip, which is used to horizontally align arbitrary symbols in vertical columns.

1,3Align => will align on the =>, for example. You can get extremely detailed with the ordering etc by using the AlignCtrl function, but the extent of its functionality is likely left to its documentation. :)

Upvotes: 8

Related Questions