Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align list items in vim [duplicate]

Say I have PHP code which looks like this

$values = array(
    'foo' => 'bar',
    'longfoo' => 'otherbar',
    'baz' => 'foobar,
);

Is there a way in Vim to align the array values and format it like following?

$values = array(
    'foo'       => 'bar',
    'longfoo'   => 'otherbar',
    'baz'       => 'foobar,
);

Minor note: I use spaces, not tabs for indenting.

like image 844
Michael Härtl Avatar asked Dec 30 '25 17:12

Michael Härtl


1 Answers

The tabular plugin would make that easy. After installing that you could simply run

:Tabularize /=>/

while on any of the lines which are to be aligned. If you use that same pattern often, you could even setup a mapping to do that even faster.

like image 178
qqx Avatar answered Jan 01 '26 17:01

qqx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!