开发者

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开发者_StackOverflow社区     => { :with => email_regex },
                  :uniqueness => { :case_sensitive => false }


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. :)


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜