如何在vim中快速自动格式化

rn0zuynd  于 2022-11-11  发布在  其他
关注(0)|答案(2)|浏览(166)

作为一个vim的初学者,我在格式化我的代码时遇到了一些问题。这里有一个小例子来解释我想要什么。下面的代码是我的原始文件,没有任何格式。它看起来太拥挤了


# include <stdio.h>

static int global=1;
static void test(int a,int b);            
int main(){   
    int local=1;
    int useless=local+1;
    printf("hello,world\n");
    test(1,2);
}
static void test(int a,int b){
    int c=a*b+a-b;
    return;
    }

我想要样式看起来像:

int useless = local + 1;

test(1 , 2);

我在命令模式下尝试:!indent --linux-style %,得到


# include <stdio.h>

static int global = 1;
static void test(int a, int b);
int main()
{
    int local = 1;
    int useless = local + 1;
    printf("hello,world\n");
    test(1, 2);
}
static void test(int a, int b)
{
    int c = a * b + a - b;
    return;
}

虽然它的工作,有两个恼人的点对我来说:
1.!indent --linux风格的%太长,效率较低
1.我想在每次输入一行时自动格式化,例如test(1,2)-〉test(1,2);
那么还有其他的解决办法吗?

0aydgbwb

0aydgbwb1#

为了满足您的需要,您可以使用autocmd

au BufWritePre *.cpp,*.c %!indent --linux-style

它将在每个文件保存前运行命令

相关问题