Format code with astyle

Source: Internet
Author: User

Sometimes from the InternetCopyCome onCodeThe format is not very friendly. I am not happy to modify a line. BeforeWindowsUseVSBuilt-inAlt + F8It's really amazing. Of course there areCodeblocksOfAstyle ......

NowLinuxI also wantThis way,VSNo,AstyleYes.......

1,AstyleIntroduction

URL:Http://astyle.sourceforge.net/

The source code is provided here. You can download it and decompress it to install it.

Style options:

View code

 Bracket style options: ---------------------- -- style = Allman or -- style = ANSI or -- style = BSD or -- style = Break Or-A1 Allman style formatting/indenting. Broken brackets. -- style = Java or -- style = Attach or-A2 Java style formatting/indenting. Attached brackets. -- style = Kr or -- style = K & R or -- style =K/R or-A3 kernighan & Ritchie style formatting/indenting. Linux brackets. -- style = Stroustrup or-A4 stroustrup style formatting/indenting. stroustrup brackets. -- style = Whitesmith or-A5 whitesmith style formatting/indenting. Broken , Indented brackets. indented class blocks and switch blocks. -- style = Banner or-A6 banner style formatting/indenting. Attached , Indented brackets. indented class blocks and switch blocks. -- style =GNU or-A7 GNU style formatting/indenting. Broken brackets , Indented blocks. -- style = Linux or-A8 Linux style formatting/indenting. Linux brackets , Minimum conditional indent is one-half indent. -- style = Horstmann or-A9 horstmann style formatting/indenting. Run-in brackets , Indented switches. -- style = 1tbs or -- style = Otbs or-A10 one true brace style formatting/indenting. Linux brackets , Add brackets to all conditionals. -- style = Pico or-A11 Pico style formatting/indenting. Run-in opening brackets and attached closing brackets. Uses keep one line blocks and keep one line statements. -- style = Lisp or-A12 lisp style formatting/indenting. Attached opening brackets and attached closing brackets. Uses keep one line statements. 

2,BashUsed in

Source code:

View code

# Include <stdio. h>IntMain (){IntI; printf ("Just a test! \ N");For(I =0; I <10; ++ I) printf ("% D \ n", I );}Return 0;}

Run the following command:Astyle test1.c

The effect is as follows:

View code

# Include <stdio. h> Int  Main (){  Int  I; printf (  "  Just a test! \ N  "  );  For (I = 0 ; I < 10 ; ++ I) {printf (  " % D \ n  "  , I );}  Return  0  ;} 

Of course there are other options:

Astyle -- style = BSD test1.c

Astyle -- style = GNU test1.c

......

3,InVimUsed in 

:%! Astyle(Simple case-astyle default mode is C/C ++) 

Or 

:%! Astyle -- mode = c -- style = ANSI-S2(Ansi c ++ style, use two spaces per indent level) 

Or 

:1, 40! Astyle -- mode = c -- style = ANSI(Ansi c ++ style, filter only lines 1-40) 

4,Batch formatting

BashThe command is as follows:

ForFIn$ (Find.-Name'*. C'-Or-name'*. Cpp'-Type F );DoAstyle $ F;Done

That isBashScript:

View code

#! /Bin/BashForFIn$ (Find.-Name'*. C'-Or-name'*. Cpp'-Type F)DoAstyle $ FDone

Okay, that's all. I hope it will help you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.