dvdvorle
dvdvorle

Reputation: 961

How to autoformat (not just auto-indent) in vim?

Is there a way to let gVim Autoformat my (c#) code? I'm not just talking about indenting, but actually formatting.

Like changing

public void Program() {
    ...
}

to

public void Program ()
{
    ...
}

and the other way around. Be it a macro, plugin or something else (formatexpr?).

I'm trying to imitate the Visual Studio formatting here. I'd love to type } and have everything look nice.

Upvotes: 3

Views: 1379

Answers (2)

mb14
mb14

Reputation: 22596

The command gq formats the code. If you are not happy with standart format rules (:help formatexpr) I guess you can use you own code formatter (via formatprg) To indent C (or similar languages) you can also read about vim native c-indent, :help C-indenting. The cindent method should be able to do what you want.

Upvotes: 0

Benoit
Benoit

Reputation: 79155

Vim has no native way to do that. However you might be interested in AStyle which has many options

astyle --help will tell you what options are available. Many presets are available.

In vim you can filter a document with :%!command where command receives the current buffer in stdin; the current buffer is replaced with the output of your command. If the command does not read from stdin but expects a file name as argument instead, use %.

Thus:

:%!C:\astyle\bin\astyle.exe -A1

will do it (provided you replace with the good path).

Upvotes: 1

Related Questions