Vim、Tabular和Ruby 1.9哈希

8
假设我在Ruby 1.9.x中的一个方法中,有以下选项哈希作为参数:
my_method :arg1,
  some_option: "value 1"
  some_other_option:true
  option_three: 123

使用Tabular VIM插件,要使选项哈希表格对齐,正则表达式应该是什么?
my_method :arg1,
  some_option:       "value 1"
  some_other_option: true
  option_three:      123
< p > 与 JSON 不同,< code >:必须保留在键上。

也许更具视觉吸引力的样式是这样的,看起来更加对齐:

my_method :arg1,
        some_option: "value 1"
  some_other_option: true
       option_three: 123

有没有人知道如何使用表格实现这些对齐方式?

谢谢!

2个回答

13
为了获得第一次对齐,可以使用以下命令。
:Tab/\w:\zs/l0l1

要将哈希键右对齐,似乎不可避免地需要在应用:Tabular命令之前仅选择包含它们的行。

:Tab/\w:\zs/r0l1l0

请注意,这些命令可以正确处理方法调用参数或哈希值包含符号(例如问题示例片段中的 :arg1)的情况。 - ib.
这仅适用于每行只有一个“:”字符的情况。 - Adam Lassek

1

我经常使用Tabular,但从未尝试过其“高级”功能。与您的示例类似的情况在:help tabular中介绍:

:Tabularize /,/r1c1l0

        Some short phrase , some other phrase
A much longer phrase here , and another long phrase

它使用所谓的“格式说明符”。

因此,将此命令应用于选项(在视觉选择后)就可以解决问题:

:'<,'>Tabularize /:/r1c1l0

my_method :arg1,
        some_option : "value 1"
  some_other_option : true
       option_three : 123

提醒自己:多玩一下Tabular。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接