如何在 vim 中自动格式化/缩进 C 代码?
当我从另一个文件复制代码时,格式会变得混乱,如下所示:
fun()
{
for(...)
{
for(...)
{
if(...)
{
}
}
}
}
How can I autoformat this code in vim?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
尝试以下击键:
说明:
gg
转到文件顶部,=
是修复缩进的命令,G
告诉它执行操作到文件末尾。Try the following keystrokes:
Explanation:
gg
goes to the top of the file,=
is a command to fix the indentation andG
tells it to perform the operation to the end of the file.我喜欢使用“艺术风格”程序。根据他们的网站:
它可以在 Windows、Linux 和 Mac 上运行。它将执行诸如缩进、用空格替换制表符或反之亦然、在操作周围放置空格等操作(将
if(x<2)
转换为 if( x<2 )
code> 如果您喜欢的话),将大括号与函数定义放在同一行,或者将它们移到下面的行等。所有选项都由命令行参数控制。为了在 vim 中使用它,只需为其设置 formatprg 选项,然后使用 gq 命令。因此,例如,我在 .vimrc: 中有这样的内容,
这样每当我打开 .cpp 文件时,formatprg 都会使用我喜欢的选项进行设置。然后,我可以输入 gg 转到文件顶部,然后输入 gqG 根据我的标准格式化整个文件。如果我只需要重新格式化单个函数,我可以转到该函数的顶部,然后输入 gq][ 并且它将重新格式化该函数。
我对 astyle 的选项
-T4pb
只是我的偏好。您可以查看他们的文档,并更改选项以使其按照您喜欢的方式格式化代码。这是一个演示。 astyle 之前:
astyle 之后 (gggqG):
I like to use the program Artistic Style. According to their website:
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.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:
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.
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.Here's a demo. Before astyle:
After astyle (gggqG):
已经提到了用于正确缩进代码的内置命令 (
gg=G
)。如果您想美化代码,则需要使用外部应用程序,例如 缩进。由于%
表示 ex 模式下的当前文件,因此您可以像这样使用它: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:我发现
clang-format
效果很好。clang 文档中有一些示例键绑定,
我更喜欢使用
equalprg< /code> 在 vim 中绑定。这允许您使用
G=gg
或其他=
缩进选项调用clang-format
。只需将以下内容放入您的 .vimrc 文件中:
I find that
clang-format
works well.There are some example keybindings in the clang documentation
I prefer to use the
equalprg
binding in vim. This allows you to invokeclang-format
withG=gg
or other=
indent options.Just put the following in your .vimrc file:
插件 vim-autoformat 允许您使用单个命令格式化缓冲区(或缓冲区选择): https: //github.com/vim-autoformat/vim-autoformat。它使用外部格式程序来实现此目的,并回退到 vim 的缩进功能。
The plugin vim-autoformat lets you format your buffer (or buffer selections) with a single command: https://github.com/vim-autoformat/vim-autoformat. It uses external format programs for that, with a fallback to vim's indentation functionality.
我喜欢上面提到的
缩进
,但大多数情况下我只想格式化我正在处理的文件的一小部分。由于indent
可以从 stdin 获取代码,因此它非常简单::!indent
进行格式化。astyle
也接受标准输入,因此您可以在那里使用相同的技巧。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. Sinceindent
can take code from stdin, its really simple::!indent
.astyle
takes stdin too, so you can use the same trick there.我想补充一点,为了防止它一开始就被搞乱,你可以在粘贴之前输入
:set Paste
。粘贴后,您可以输入:set nopaste
来执行 js-beautify 和缩进等操作,以便再次使用。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.也许你可以尝试以下方法
$indent -kr -i8 *.c
希望对您有用!
Maybe you can try the followings
$indent -kr -i8 *.c
Hope it's useful for you!
他们有一个名为
indent
的工具。您可以使用apt-get install indent
下载它,然后运行indent my_program.c
。Their is a tool called
indent
. You can download it withapt-get install indent
, then runindent my_program.c
.要对此处提到的许多选项进行良好的概述和演示,@Gavin-Freeborn 在 YouTube 上有一个很棒的视频:
https://www.youtube.com/watch?v=tM_uIwSucPU
它涵盖了一些 Vim 插件以及内置功能,例如
=
、gq< /code> 和
formatprg
。For a good overview and demo of many of the options mentioned here, @Gavin-Freeborn has a great video on YouTube:
https://www.youtube.com/watch?v=tM_uIwSucPU
It covers some Vim plugins as well as built-in capabilities such as
=
,gq
, andformatprg
.