Vim, Tabular, and Ruby 1.9 Hash

Assuming I have the following Hash options as a method argument in Ruby 1.9.x:

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

Using the Tabular VIM plugin, what will the regex be for the Hash parameters to align like this:

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

: must remain attached to the key, unlike, for example, JSON.

Perhaps a more attractive style will look that way that looks more aligned:

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

Does anyone happen to know how to do one of these alignments using Tabular?

Thanks!

+8
vim alignment hash tabular
source share
2 answers

To get the first alignment, you can use the command

 :Tab/\w:\zs/l0l1 

To align the hash keys to the right, it seems inevitable to select only the lines containing them before applying the command :Tabular ,

 :Tab/\w:\zs/r0l1l0 
+13
source share

I use Tabular a lot, but I have never found a chance to try my advanced features. A case similar to your example is presented in :help tabular :

 :Tabularize /,/r1c1l0 Some short phrase , some other phrase A much longer phrase here , and another long phrase 

which uses what it calls "format specifiers".

So, applying this command to parameters (after visual selection) will do the trick:

 :'<,'>Tabularize /:/r1c1l0 my_method :arg1, some_option : "value 1" some_other_option : true option_three : 123 

Note to yourself: play more with the table.

+1
source share

All Articles