The settings I used in my vimrc for code formatting:

" Need astyle installed
autocmd BufNewFile,BufRead *.c set formatprg=astyle\ -A1xjxVpSNYk1Hm0xwxWO
autocmd BufNewFile,BufRead *.cpp set formatprg=astyle\ -A1xjxVpSNYk1Hm0xwxWO
" Uses mz to mark in register z
nnoremap <F7> mzgggqG`z

Below are the meanings of the arguments in -A1xjxVpSNYk1Hm0xwxWO

-A1 for broken braces. `--style=allman / --style=bsd / --style=break / -A1` Allman style uses broken braces. ```c int Foo(bool isBar) { if (isBar) { bar(); return 1; } else return 0; } ```
A2 for attached braces. (not used, can be used to replace -A1) `--style=java / --style=attach / -A2` Java style uses attached braces. ```c int Foo(bool isBar) { if (isBar) { bar(); return 1; } else return 0; } ```
-xj for single statement in braces. `--remove-braces / -xj` Remove braces from conditional statements (e.g. 'if', 'for', 'while'...). The statement must be a single statement on a single line. If --add-braces or --add-one-line-braces is also used the result will be to add braces. Braces will not be removed from "One True Brace Style", --style=1tbs. ```c if (isFoo) { isFoo = false; } becomes: if (isFoo) isFoo = false; ```
-xV for attach do-while. `--attach-closing-while / -xV` Attach the closing 'while' of a 'do-while' statement to the closing brace. This has precedence over both the brace style and the break closing braces option. ```c do { bar(); ++x; } while x == 1; becomes: do { bar(); ++x; } while x == 1; ```
-p for paddings around operators and commas. `--pad-oper / -p` Insert space padding around operators. This will also pad commas. Any end of line comments will remain in the original column, if possible. Note that there is no option to unpad. Once padded, they stay padded. ```c if (foo==2) a=bar((b-c)*a,d--); becomes: if (foo == 2) a = bar((b - c) * a, d--); ```
-S for case indent in switch-case. `--indent-switches / -S` Indent 'switch' blocks so that the 'case X:' statements are indented in the switch block. The entire case block is indented. ```c switch (foo) { case 1: a += 1; break; case 2: { a += 2; break; } } becomes: switch (foo) { case 1: a += 1; break; case 2: { a += 2; break; } } ```
-N for namespace indentation. `--indent-namespaces / -N` Add extra indentation to namespace blocks. This option has no effect on Java files. It will also indent CORBA IDL module statements. ```cpp namespace foospace { class Foo { public: Foo(); virtual ~Foo(); }; } becomes: namespace foospace { class Foo { public: Foo(); virtual ~Foo(); }; } ```
-Y for comments in column one. `--indent-col1-comments / -Y` Indent C++ comments beginning in column one. By default C++ comments beginning in column one are assumed to be commented‑out code and not indented. This option will allow the comments to be indented with the code. ```c void Foo() { // comment if (isFoo) bar(); } becomes: void Foo() { // comment if (isFoo) bar(); } ```
-k1 for alignment of pointer syntaxes: *, &, ^. ``` --align-pointer=type / -k1 --align-pointer=middle / -k2 --align-pointer=name / -k3 ``` Attach a pointer or reference operator (*, &, or ^) to either the variable type (left) or variable name (right), or place it between the type and name (middle). The spacing between the type and name will be preserved, if possible. This option is for C/C++, C++/CLI, and C# files. To format references separately, use the following align-reference option. ```c char* foo1; char & foo2; string ^s1; becomes (with align-pointer=type): char* foo1; char& foo2; string^ s1; ``` ```c char* foo1; char & foo2; string ^s1; becomes (with align-pointer=middle): char * foo1; char & foo2; string ^ s1; ``` ```c char* foo1; char & foo2; string ^s1; becomes (with align-pointer=name): char *foo1; char &foo2; string ^s1; ```
-H for space padding after headers (e.g. ‘if’, ‘for’, …). `--pad-header / -H` Insert space padding between a header (e.g. 'if', 'for', 'while'...) and the following paren. Any end of line comments will remain in the original column, if possible. This can be used with unpad-paren to remove unwanted spaces. ```c if(isFoo((a+2), b)) bar(a, b); becomes: if (isFoo((a+2), b)) bar(a, b); ```
-m0 for headers with multiple lines. `--min-conditional-indent=# / -m#` Set the minimal indent that is added when a header is built of multiple lines. This indent helps to easily separate the header from the command statements that follow. The value for # indicates a number of indents and is a minimum value. The indent may be greater to align with the data on the previous line. The valid values are: 0 - no minimal indent. The lines will be aligned with the paren on the preceding line. 1 - indent at least one additional indent. 2 - indent at least two additional indents. 3 - indent at least one-half an additional indent. This is intended for large indents (e.g. 8). The default value is 2, two additional indents. ```c // default setting makes this non-braced code clear if (a < b || c > d) foo++; // but creates an exaggerated indent in this braced code if (a < b || c > d) { foo++; } becomes (when setting --min-conditional-indent=0): // setting makes this non-braced code less clear if (a < b || c > d) foo++; // but makes this braced code clearer if (a < b || c > d) { foo++; } ```
-xw for indentation of preprocessor statements. `--indent-preproc-cond / -xw` Indent preprocessor conditional statements to the same level as the source code. ```c isFoo = true; #ifdef UNICODE text = wideBuff; #else text = buff; #endif becomes: isFoo = true; #ifdef UNICODE text = wideBuff; #else text = buff; #endif ```
-xW for indentation of preprocessor conditional statements. `--indent-preproc-block / -xW` Indent preprocessor blocks at brace level zero and immediately within a namespace. There are restrictions on what will be indented. Blocks within methods, classes, arrays, etc., will not be indented. Blocks containing braces or multi-line define statements will not be indented. Without this option the preprocessor block is not indented. ```c #ifdef _WIN32 #include #ifndef NO_EXPORT #define EXPORT #endif #endif becomes: #ifdef _WIN32 #include #ifndef NO_EXPORT #define EXPORT #endif #endif ``` </details>
-O for keeping one-line blocks. (for struct initialization POINT p = {10, 20}; ) `--keep-one-line-blocks / -O` Don't break one-line blocks. ```c if (isFoo) { isFoo = false; cout << isFoo << endl; } remains unchanged. ```
-s4 (not used, since -s4 is default.) `--indent=spaces / --indent=spaces=# / -s#` Indent using # spaces per indent (e.g. -s3 --indent=spaces=3). # must be between 2 and 20. Not specifying # will result in a default of 4 spaces per indent. with indent=spaces=3 ```c void Foo() { ...if (isBar1 .........&& isBar2) // indent of this line can be changed with min-conditional-indent ......bar(); } ```
For full arguments see: [Artistic Style Documentation](http://astyle.sourceforge.net/astyle.html) ## Further Reading 1. [AStyle \| SourceForge](http://astyle.sourceforge.net/astyle.html)

Posted: