C语言 如何在 vim 中自动格式化/缩进 C 代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2355834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I autoformat/indent C code in vim?
提问by Yongwei Xing
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 中自动格式化此代码?
回答by Amir Rachum
Try the following keystrokes:
尝试以下按键:
gg=G
Explanation: gggoes to the top of the file, =is a command to fix the indentation and Gtells it to perform the operation to the end of the file.
说明:gggo to the top of the file,=是一个修复缩进的命令,G告诉它执行到文件末尾的操作。
回答by Derek
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.
Artistic Style 是 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.
它可以在 Window、Linux 和 Mac 上运行。它将执行诸如缩进、用空格替换制表符或反之亦然、根据您的喜好在操作周围放置空格(转换if(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 gqGto 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.
希望有帮助。
回答by soulmerge
The builtin command for properly indenting the code has already been mentioned (gg=G). If you want to beautifythe 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)。如果你想美化代码,你需要使用像indent这样的外部应用程序。由于%表示 ex 模式下的当前文件,您可以像这样使用它:
:!indent %
回答by Chiel ten Brinke
The plugin vim-autoformat lets you format your buffer (or buffer selections) with a single command: 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 的缩进功能。
回答by Daniel
I find that clang-formatworks well.
我觉得这clang-format很好用。
There are some example keybindings in the clang documentation
在clang 文档中有一些示例键绑定
I prefer to use the equalprgbinding in vim. This allows you to invoke clang-formatwith G=ggor other =indent options.
我更喜欢equalprg在 vim 中使用绑定。这允许您调用clang-formatwithG=gg或其他=缩进选项。
Just put the following in your .vimrc file:
只需将以下内容放入您的 .vimrc 文件中:
autocmd FileType c,cpp setlocal equalprg=clang-format
回答by dsummersl
I like indentas mentioned above, but most often I want to format only a small section of the file that I'm working on. Since indentcan take code from stdin, its really simple:
我喜欢indent上面提到的,但大多数时候我只想格式化我正在处理的文件的一小部分。由于indent可以从标准输入中获取代码,它非常简单:
- Select the block of code you want to format with Vor the like.
- Format by typing
:!indent.
- 选择您想要格式化的代码块V等。
- 通过键入格式化
:!indent。
astyletakes stdin too, so you can use the same trick there.
astyle也需要标准输入,所以你可以在那里使用相同的技巧。
回答by lattimore
Maybe you can try the followings $indent -kr -i8 *.c
也许你可以试试下面的 $indent -kr -i8 *.c
Hope it's useful for you!
希望对你有用!
回答by atripes
I wanted to add, that in order to prevent it from being messed up in the first place you can type :set pastebefore pasting. After pasting, you can type :set nopastefor things like js-beautify and indenting to work again.
我想补充一点,为了防止它被弄乱,你可以:set paste在粘贴之前先输入。粘贴后,您可以输入:set nopaste诸如 js-beautify 和缩进之类的内容以再次工作。
回答by n0va_sa
Their is a tool called indent. You can download it with apt-get install indent, then run indent my_program.c.
他们是一个名为indent. 您可以使用 下载它apt-get install indent,然后运行indent my_program.c。

