Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: source code formatting

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

like image 391
Stas Avatar asked Nov 10 '10 08:11

Stas


People also ask

How do I format in Vim?

It's possible to reformat an entire file, or a section of a file, using Vim's built-in = filter. Vim veterans often find this operator to be one of the most useful in their repertoire, but so common that it becomes second-nature and is rarely mentioned. In normal mode, typing gg=G will reindent the entire file.

How do I indent C in Vim?

Select the block of code you want to format with V or the like. Format by typing :! indent .

Does Vim have auto indentation?

To automatically indent when editing a file in Vim, enable the auto indenting feature using the :set autoindent flag in command mode: Press Enter, and this will auto-indent the file you are currently editing. If you set the auto-indent feature in Vim in command mode, it does not persist upon closing the editor.


1 Answers

You can harvest from two plug-ins that can do this stuff:

  • Align.vim , or
  • Tabular.vim
like image 121
Benoit Avatar answered Sep 24 '22 03:09

Benoit