search

Home  >  Q&A  >  body text

How to quickly format this code in vim?

#include<stdio.h>
int main(int argc, char *argv[])
{
int i=0;
if (i != 0) {i++; }
else {i--; };
for(i=0;i<5;i++)j++;
printf("i=%d,j=%d\n",i,j);
return 0;
}

上面的代码如何快速格式化成这个样子?

#include 
int main(int argc ,char* argv[])
{
    int a = 4;
    int b = 5;
    int f_a = factorial(a);
    int f_b = factorial(b);
    while(1)
    {
        if(cmp(f_a,f_b))
        {
            printf("f_a is bigger than f_b\n");
        }
        else
        {
            printf("f_a is not bigger than f_b\n");
        }
        sleep(100);
    }     
    return 0;
    }
    
    不用插件,自己可否写一个简单的模板文件,用来格式化文件?
    
    10086的解答结果不对。
    ggvG
    =
    变成
    
#include<stdio.h>
int main(int argc, char *argv[])
{
        int i=0;
        if (i != 0) {i++; }
        else {i--; };
        for(i=0;i<5;i++)j++;
        printf("i=%d,j=%d\n",i,j);
        return 0;
}
高洛峰高洛峰2776 days ago643

reply all(9)I'll reply

  • 仅有的幸福

    仅有的幸福2017-05-16 16:35:49

    Actually, it has a lot to do with the original writing method.
    #include<stdio.h>
    int main(int argc, char *argv[])
    {
    for(i in f){
    if(i ==9 ){
    printf("1");
    } else{
    printf("2");
    }
    }
    }
    If you write like the above, the result of using gg=G is cool:
    #include<stdio.h>
    int main(int argc, char *argv[])
    {

    for(i in f){
        if(i ==9 ){
            printf("1");
        }else{
            printf("2");
        }
    }

    }

    It’s perfect.

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-05-16 16:35:49

    gg=G

    reply
    0
  • ringa_lee

    ringa_lee2017-05-16 16:35:49

    In fact, what I use more is, in visual mode, move to the beginning of the line and press: > That is, press 'shift + .' at the same time

    reply
    0
  • 黄舟

    黄舟2017-05-16 16:35:49

    !astyle --style=[the format you want] -p -H --suffix=none %

    If you installed astyle

    reply
    0
  • PHPz

    PHPz2017-05-16 16:35:49

    Step one:ggvG ///< 先回到首行, 然后选择模式, 全选代码
    第二步:= ///< = Generally used for equal layout, here represents code indentation. Under vim with multiple splites, you can also adjust the size of each splite equally

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-16 16:35:49

    gg
    vG
    =

    reply
    0
  • 高洛峰

    高洛峰2017-05-16 16:35:49

    Yes, 10086 and tdblq did not pay attention to the question’s requirement for brackets.

    Give me the order first

    %s/ {/^M{^M/g
    %s/ }/^M}/g
    %s/};/}/g
    %s/)\([^;]\+\);/)^M;/g
    gg
    =G

    Line 1~3 is dealing with the problem of curly braces.

    Line 4 is for solution

    for(i=0;i<5;i++)j++;
    变成
    for(i=0;i<5;i++)
        j++;

    Lines 5~6 are the full text automatically indented.
    Note The ^M is not entered directly, but obtained by "ctrl+v"+"Enter".

    But these are just solving the problem of format.
    The questioner said that he needs to "format" the code from a for loop to a code with while.
    I guess it is a typo?

    reply
    0
  • ringa_lee

    ringa_lee2017-05-16 16:35:49

    In fact, strictly speaking, vim cannot handle it. All gg=G can do is adjust the indentation in units of lines. Really needing external tools like lz, such as indent. Assume you have a perfect indent configuration. Then you can use %!indent to do it in vim

    reply
    0
  • PHP中文网

    PHP中文网2017-05-16 16:35:49

    Select the lines you want to indent, press the big V to enter the line mode, and then directly press >right indent, <left indent, and just arrange them line by line_(:з ∠)_

    reply
    0
  • Cancelreply