Take a look at the enum:
enum TestEnum
{
First = 1,
Second = 2,
Unknown = 3,
TestTestTest = 100,
Zero = 0,
Foo = 123,
}
How can I use the whole power of Vim to reformat it?
enum TestEnum
{
First = 1,
Second = 2,
Unknown = 3,
TestTestTest = 100,
Zero = 0,
Foo = 123,
}
Personally, I'm moving line by line and tabbing. It is the same as I would do that in any regular editor. How to do that the right way?
The same for class members:
class Foo
{
SuperFoo foo1;
RegularFoo foo2;
SuperiorFoo foo3;
YetAnotherFoo foo4;
Bar bar;
}
to something like
class Foo
{
SuperFoo foo1;
RegularFoo foo2;
SuperiorFoo foo3;
YetAnotherFoo foo4;
Bar bar;
}
Thanks
You can harvest from two plug-ins that can do this stuff:
- Align.vim
, or
- Tabular.vim
The Align.vim plugin is probably the way to go, but if you wish to have it handy on a standard installation, you could always filter through awk to get some generic functionality with not too much work.
For TestEnum you would do something like
'<,'>!awk '{printf "^I\%-20s\%-20s\%-20s\n", $1, $2, $3}'
after visually selecting the braced contents (viB is awesome here.)
For Foo you would do
'<,'>!awk '{printf "^I\%-20s\%-20s\n", $1, $2}'
You could probably make it variable width with an awk for-loop but at the cost of the easy and fast to type version here.
If you have the unix utility col
handy, you might simply try
'<,'>!col -x
But here your mileage will really vary, as this is not the intended use of the utility.
Align or Tabular sound 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.