How to quickly format this code in vim?
高洛峰
高洛峰 2017-05-16 16:33:49
0
9
606
#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;
}
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(9)
仅有的幸福

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.

phpcn_u1582
gg=G
左手右手慢动作

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

黄舟

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

If you installed astyle

PHPzhong

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

小葫芦

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?

左手右手慢动作

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

洪涛

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_(:з ∠)_

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template