zoukankan      html  css  js  c++  java
  • 用astyle格式化代码

    有时候从网上copy来的代码格式不怎么友好,一行行的改,我实在不乐意。之前在windows下用VS自带的ALT + F8着实爽了一把,当然还有CodeBlocksastyle……

    现在linux下我也想这样VS是不用想了,astyle倒是可以……

    1astyle介绍

    网址http://astyle.sourceforge.net/

    这里有源码,自己下载后解压安装下就可以了。

    风格选项

    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.

    2bash中使用

    如下源码:

    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;}

    执行命令:astyle test1.c

    效果如下:

    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;
    }

    当然还有其他选项:

    astyle --style=bsd test1.c

    astyle --style=gnu test1.c

    ……

    3vim中使用 

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

    或者 

    :%!astyle --mode=c --style=ansi -s2 (ansi C++ style, use two spaces per indent level) 

    或者 

    :1,40!astyle --mode=c --style=ansi (ansi C++ style, filter only lines 1-40) 

    4批量格式化

    bash命令如下:

    for f in $(find . -name '*.c' -or -name '*.cpp' -type f); do astyle $f; done

    即如下bash脚本:

    View Code
    #! /bin/bash
    
    for f in $(find . -name '*.c' -or -name '*.cpp' -type f)
    do
    astyle $f
    done

    好,就这些了,希望对你有帮助。

  • E-Mail : Mike_Zhang@live.com
  • 转载请注明出处,谢谢!
查看全文
  • 相关阅读:
    [Luogu P4178]Tree 题解(点分治+平衡树)
    [20190725NOIP模拟测试8]题解
    暑假集训考试反思+其它乱写
    [bzoj2752]高速公路 题解(线段树)
    bzoj1211树的计数 x bzoj1005明明的烦恼 题解(Prufer序列)
    [CQOI2014]数三角形 题解(找规律乱搞)
    [Catalan数三连]网格&有趣的数列&树屋阶梯
    [NOIP模拟测试7]visit 题解(组合数学+CRT+Lucas定理)
    [7.22NOIP模拟测试7]方程的解 题解(扩展欧几里得)
    leetcode371
  • 原文地址:https://www.cnblogs.com/MikeZhang/p/useAstyleInLinux.html
  • Copyright © 2011-2022 走看看