C.C
C.C

Reputation: 89

How to auto-format in vim quickly

As a beginner for vim , I met some problem when format my code. Here is a tiny example to explain what I want.The code below is my original file without any formatting.It looks too crowd

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

I want style look like:

int useless = local + 1;

or

test(1 , 2);

I try :!indent --linux-style % in command mode and gets

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

Though it works, there are two annoying points for me:

  1. !indent --linux-style % is too long and less efficent
  2. I want to auto format each time I input one single line,such as test(1,2) -> test(1, 2);

So is there other solution ? THX

Upvotes: 0

Views: 2698

Answers (2)

C.C
C.C

Reputation: 89

I try this plugin: https://github.com/vim-autoformat/vim-autoformat

and solved my problem

Upvotes: 0

kosciej16
kosciej16

Reputation: 7128

To satisfy your need you could just use autocmd

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

It will run command before each file save

Upvotes: 1

Related Questions