如何在vim中autoformat/indent C代码?

时间:2022-11-25 10:53:33

When I copy code from another file, the formatting is messed up, like this:

当我从另一个文件中复制代码时,格式就乱了,像这样:

fun()
{
for(...)
{
for(...)
{
if(...)
{
}
}
}
}

How can I autoformat this code in vim?

如何在vim中自动格式化此代码?

10 个解决方案

#1


452  

Try the following keystrokes:

试试下面的按键:

gg=G

Explanation: gg goes to the top of the file, = is a command to fix the indentation and G tells it to perform the operation to the end of the file.

说明:gg进入文件的顶部,=是一个用来修复缩进的命令,G告诉它将操作执行到文件的末尾。

#2


73  

I like to use the program Artistic Style. According to their website:

我喜欢使用程序的艺术风格。根据他们的网站:

Artistic Style is a source code indenter, formatter, and beautifier for the C, C++, C# and Java programming languages.

艺术风格是为C、c++、c#和Java编程语言编写的源代码、格式化程序和美化程序。

It runs in Window, Linux and Mac. It will do things like indenting, replacing tabs with spaces or vice-versa, putting spaces around operations however you like (converting if(x<2) to if ( x<2 ) if that's how you like it), putting braces on the same line as function definitions, or moving them to the line below, etc. All the options are controlled by command line parameters.

它在窗口,运行Linux和Mac。它会缩进、制表符替换为空格或亦然,将周围的空间操作但是你喜欢(转换如果(x < 2)(x < 2)如果你喜欢它),把括号在同一行函数定义,或移动他们下面的线,等。所有的选项都由命令行参数控制。

In order to use it in vim, just set the formatprg option to it, and then use the gq command. So, for example, I have in my .vimrc:

为了在vim中使用它,只需将formatprg选项设置为它,然后使用gq命令。例如,我的。vimrc:

autocmd BufNewFile,BufRead *.cpp set formatprg=astyle\ -T4pb

so that whenever I open a .cpp file, formatprg is set with the options I like. Then, I can type gg to go to the top of the file, and gqG to format the entire file according to my standards. If I only need to reformat a single function, I can go to the top of the function, then type gq][ and it will reformat just that function.

因此,每当我打开.cpp文件时,formatprg就设置了我喜欢的选项。然后,我可以键入gg到文件的顶部,gqG根据我的标准格式化整个文件。如果我只需要重新格式化一个函数,我就可以到达函数的顶部,然后键入gq][它将重新格式化这个函数。

The options I have for astyle, -T4pb, are just my preferences. You can look through their docs, and change the options to have it format the code however you like.

我对astyle, -T4pb的选择只是我的偏好。您可以查看他们的文档,并更改选项以使其格式化您喜欢的代码。

Here's a demo. Before astyle:

这是一个演示。astyle之前:

int main(){if(x<2){x=3;}}

float test()
{
if(x<2)
x=3;
}

After astyle (gggqG):

后astyle(gggqG):

int main()
{
    if (x < 2)
    {
        x = 3;
    }
}

float test()
{
    if (x < 2)
        x = 3;
}

Hope that helps.

希望有帮助。

#3


50  

The OP asked for auto-formatting, but accepted an answer that does auto-indenting only.

OP要求自动格式化,但只接受自动缩进的答案。

Here is the difference:

这是区别:

ORIGINAL

原始

int main(){if(x<2){x=3;}}

float test()
{
if(x<2)
x=3;
}

AUTOINDENT

AUTOINDENT

int main(){if(x<2){x=3;}}

float test()
{
    if(x<2)
        x=3;
}

AUTOFORMAT

自动套用格式

int main()
{
    if (x < 2)
    {
        x = 3;
    }
}

float test()
{
    if (x < 2)
        x = 3;
}

#4


24  

The builtin command for properly indenting the code has already been mentioned (gg=G). If you want to beautify the code, you'll need to use an external application like indent. Since % denotes the current file in ex mode, you can use it like this:

已经提到了正确地缩进代码的内置命令(gg=G)。如果您想美化代码,您需要使用像缩进这样的外部应用程序。由于%表示当前文件在ex模式下,您可以这样使用:

:!indent %

#5


12  

The plugin vim-autoformat lets you format your buffer with only one button press: https://github.com/Chiel92/vim-autoformat. It uses external format programs for that, with a fallback to vim's indentation functionality.

插件vim-autoformat允许您只使用一个按钮按下格式化缓冲区:https://github.com/Chiel92/vim-autoformat。它使用外部格式程序来实现这一点,并使用了vim的缩进功能。

#6


6  

I find that clang-format works well.

我发现clang-format很好用。

There are some example keybindings in the clang documentation

在clang文档中有一些示例keybindings。

I prefer to use the equalprg binding in vim. This allows you to invoke clang-format with G=gg or other = indent options.

我更喜欢在vim中使用equalprg绑定。这允许您使用G=gg或其他=缩进选项调用clangformat。

Just put the following in your .vimrc file:

只需将以下内容放入.vimrc文件中:

autocmd FileType c,cpp setlocal equalprg=clang-format

#7


3  

I like indent as mentioned above, but most often I want to format only a small section of the file that I'm working on. Since indent can take code from stdin, its really simple:

我喜欢上面提到的缩进,但大多数情况下我只想格式化我正在处理的文件的一小部分。因为indent可以从stdin获取代码,它非常简单:

  1. Select the block of code you want to format with V or the like.
  2. 选择要格式化为V或类似的代码块。
  3. Format by typing :!indent.
  4. 格式输入:缩进。

astyle takes stdin too, so you can use the same trick there.

astyle也接受了stdin,所以你可以用同样的方法。

#8


2  

Maybe you can try the followings $indent -kr -i8 *.c

也许你可以试试下面的$indent -kr -i8 *.c。

Hope it's useful for you!

希望它对你有用!

#9


1  

I wanted to add, that in order to prevent it from being messed up in the first place you can type :set paste before pasting. After pasting, you can type :set nopaste for things like js-beautify and indenting to work again.

我想要补充的是,为了防止它在第一个地方被弄乱,你可以在粘贴之前设置粘贴。粘贴之后,您可以键入:set nopaste,用于像js-beautify和indenting这样的东西再次工作。

#10


0  

Their is a tool called indent. You can download it with apt-get indent, then run indent my_program.c.

它们是一种叫做缩进的工具。您可以通过apt-get indent下载它,然后运行indent my_program.c。

#1


452  

Try the following keystrokes:

试试下面的按键:

gg=G

Explanation: gg goes to the top of the file, = is a command to fix the indentation and G tells it to perform the operation to the end of the file.

说明:gg进入文件的顶部,=是一个用来修复缩进的命令,G告诉它将操作执行到文件的末尾。

#2


73  

I like to use the program Artistic Style. According to their website:

我喜欢使用程序的艺术风格。根据他们的网站:

Artistic Style is a source code indenter, formatter, and beautifier for the C, C++, C# and Java programming languages.

艺术风格是为C、c++、c#和Java编程语言编写的源代码、格式化程序和美化程序。

It runs in Window, Linux and Mac. It will do things like indenting, replacing tabs with spaces or vice-versa, putting spaces around operations however you like (converting if(x<2) to if ( x<2 ) if that's how you like it), putting braces on the same line as function definitions, or moving them to the line below, etc. All the options are controlled by command line parameters.

它在窗口,运行Linux和Mac。它会缩进、制表符替换为空格或亦然,将周围的空间操作但是你喜欢(转换如果(x < 2)(x < 2)如果你喜欢它),把括号在同一行函数定义,或移动他们下面的线,等。所有的选项都由命令行参数控制。

In order to use it in vim, just set the formatprg option to it, and then use the gq command. So, for example, I have in my .vimrc:

为了在vim中使用它,只需将formatprg选项设置为它,然后使用gq命令。例如,我的。vimrc:

autocmd BufNewFile,BufRead *.cpp set formatprg=astyle\ -T4pb

so that whenever I open a .cpp file, formatprg is set with the options I like. Then, I can type gg to go to the top of the file, and gqG to format the entire file according to my standards. If I only need to reformat a single function, I can go to the top of the function, then type gq][ and it will reformat just that function.

因此,每当我打开.cpp文件时,formatprg就设置了我喜欢的选项。然后,我可以键入gg到文件的顶部,gqG根据我的标准格式化整个文件。如果我只需要重新格式化一个函数,我就可以到达函数的顶部,然后键入gq][它将重新格式化这个函数。

The options I have for astyle, -T4pb, are just my preferences. You can look through their docs, and change the options to have it format the code however you like.

我对astyle, -T4pb的选择只是我的偏好。您可以查看他们的文档,并更改选项以使其格式化您喜欢的代码。

Here's a demo. Before astyle:

这是一个演示。astyle之前:

int main(){if(x<2){x=3;}}

float test()
{
if(x<2)
x=3;
}

After astyle (gggqG):

后astyle(gggqG):

int main()
{
    if (x < 2)
    {
        x = 3;
    }
}

float test()
{
    if (x < 2)
        x = 3;
}

Hope that helps.

希望有帮助。

#3


50  

The OP asked for auto-formatting, but accepted an answer that does auto-indenting only.

OP要求自动格式化,但只接受自动缩进的答案。

Here is the difference:

这是区别:

ORIGINAL

原始

int main(){if(x<2){x=3;}}

float test()
{
if(x<2)
x=3;
}

AUTOINDENT

AUTOINDENT

int main(){if(x<2){x=3;}}

float test()
{
    if(x<2)
        x=3;
}

AUTOFORMAT

自动套用格式

int main()
{
    if (x < 2)
    {
        x = 3;
    }
}

float test()
{
    if (x < 2)
        x = 3;
}

#4


24  

The builtin command for properly indenting the code has already been mentioned (gg=G). If you want to beautify the code, you'll need to use an external application like indent. Since % denotes the current file in ex mode, you can use it like this:

已经提到了正确地缩进代码的内置命令(gg=G)。如果您想美化代码,您需要使用像缩进这样的外部应用程序。由于%表示当前文件在ex模式下,您可以这样使用:

:!indent %

#5


12  

The plugin vim-autoformat lets you format your buffer with only one button press: https://github.com/Chiel92/vim-autoformat. It uses external format programs for that, with a fallback to vim's indentation functionality.

插件vim-autoformat允许您只使用一个按钮按下格式化缓冲区:https://github.com/Chiel92/vim-autoformat。它使用外部格式程序来实现这一点,并使用了vim的缩进功能。

#6


6  

I find that clang-format works well.

我发现clang-format很好用。

There are some example keybindings in the clang documentation

在clang文档中有一些示例keybindings。

I prefer to use the equalprg binding in vim. This allows you to invoke clang-format with G=gg or other = indent options.

我更喜欢在vim中使用equalprg绑定。这允许您使用G=gg或其他=缩进选项调用clangformat。

Just put the following in your .vimrc file:

只需将以下内容放入.vimrc文件中:

autocmd FileType c,cpp setlocal equalprg=clang-format

#7


3  

I like indent as mentioned above, but most often I want to format only a small section of the file that I'm working on. Since indent can take code from stdin, its really simple:

我喜欢上面提到的缩进,但大多数情况下我只想格式化我正在处理的文件的一小部分。因为indent可以从stdin获取代码,它非常简单:

  1. Select the block of code you want to format with V or the like.
  2. 选择要格式化为V或类似的代码块。
  3. Format by typing :!indent.
  4. 格式输入:缩进。

astyle takes stdin too, so you can use the same trick there.

astyle也接受了stdin,所以你可以用同样的方法。

#8


2  

Maybe you can try the followings $indent -kr -i8 *.c

也许你可以试试下面的$indent -kr -i8 *.c。

Hope it's useful for you!

希望它对你有用!

#9


1  

I wanted to add, that in order to prevent it from being messed up in the first place you can type :set paste before pasting. After pasting, you can type :set nopaste for things like js-beautify and indenting to work again.

我想要补充的是,为了防止它在第一个地方被弄乱,你可以在粘贴之前设置粘贴。粘贴之后,您可以键入:set nopaste,用于像js-beautify和indenting这样的东西再次工作。

#10


0  

Their is a tool called indent. You can download it with apt-get indent, then run indent my_program.c.

它们是一种叫做缩进的工具。您可以通过apt-get indent下载它,然后运行indent my_program.c。