如何在 vim 中自动格式化/缩进 C 代码?

发布于 2024-08-23 12:09:22 字数 156 浏览 5 评论 0原文

当我从另一个文件复制代码时,格式会变得混乱,如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

榕城若虚 2024-08-30 12:09:22

尝试以下击键:

gg=G

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

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.

罗罗贝儿 2024-08-30 12:09:22

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

Artistic Style 是 C、C++、C# 和 Java 编程语言的源代码缩进器、格式化器和美化器。

它可以在 Windows、Linux 和 Mac 上运行。它将执行诸如缩进、用空格替换制表符或反之亦然、在操作周围放置空格等操作(将 if(x<2) 转换为 if ( x<2 ) code> 如果您喜欢的话),将大括号与函数定义放在同一行,或者将它们移到下面的行等。所有选项都由命令行参数控制。

为了在 vim 中使用它,只需为其设置 formatprg 选项,然后使用 gq 命令。因此,例如,我在 .vimrc: 中有这样的内容,

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

这样每当我打开 .cpp 文件时,formatprg 都会使用我喜欢的选项进行设置。然后,我可以输入 gg 转到文件顶部,然后输入 gqG 根据我的标准格式化整个文件。如果我只需要重新格式化单个函数,我可以转到该函数的顶部,然后输入 gq][ 并且它将重新格式化该函数。

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

这是一个演示。 astyle 之前:

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

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

astyle 之后 (gggqG):

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

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

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.

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:

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.

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:

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

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

After astyle (gggqG):

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

float test()
{
    if (x < 2)
        x = 3;
}
只涨不跌 2024-08-30 12:09:22

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

:!indent %

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:

:!indent %
梦里°也失望 2024-08-30 12:09:22

我发现 clang-format 效果很好。

clang 文档中有一些示例键绑定,

我更喜欢使用 equalprg< /code> 在 vi​​m 中绑定。这允许您使用 G=gg 或其他 = 缩进选项调用 clang-format

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

autocmd FileType c,cpp setlocal equalprg=clang-format

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 invoke clang-format with G=gg or other = indent options.

Just put the following in your .vimrc file:

autocmd FileType c,cpp setlocal equalprg=clang-format
‖放下 2024-08-30 12:09:22

插件 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.

陈甜 2024-08-30 12:09:22

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

  1. 使用 V 等选择要格式化的代码块。
  2. 通过输入 :!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. Since indent can take code from stdin, its really simple:

  1. Select the block of code you want to format with V or the like.
  2. Format by typing :!indent.

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

再可℃爱ぅ一点好了 2024-08-30 12:09:22

我想补充一点,为了防止它一开始就被搞乱,你可以在粘贴之前输入 :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.

凉世弥音 2024-08-30 12:09:22

也许你可以尝试以下方法
$indent -kr -i8 *.c

希望对您有用!

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

Hope it's useful for you!

追星践月 2024-08-30 12:09:22

他们有一个名为indent的工具。您可以使用apt-get install indent下载它,然后运行indent my_program.c

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

吃素的狼 2024-08-30 12:09:22

要对此处提到的许多选项进行良好的概述和演示,@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, and formatprg.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文