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.
Domanda
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.
Soluzione
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.
Altri suggerimenti
Tabularize sounds like the way to go, but I will also mention the Unix utility column
, which is pretty nifty and more people should know about.
Unix-specific, obviously. (On openSuSE 12.3, it's in the util-linux package; likely different on other distributions.)
To invoke it within vim, visually select the lines you want to align, then
:!column -t
So with the visual range that vim fills in for you when you hit :
with lines selected, you get:
:'<,'>!column -t
(By default it separates on whitespace, but you can change that with the -s <separator>
option.)
It aligns things such that each column is just long enough for its longest member.
I suggest the plugin Align. It has some very handy shortcuts, like \tsp
could let you align columns by spaces quickly.