缩进 C 源文件行的命令

0 c source text-processing indentation

我需要一种在终端内的 C 源文件上自动缩进块的方法。根据规范。

前:

int main() {
puts("Hello world");
}
Run Code Online (Sandbox Code Playgroud)

后:

int main()
{
puts("Hello world");
}
Run Code Online (Sandbox Code Playgroud)

dha*_*hag 5

用于这项工作的经典 Unix 工具是indent(例如,GNU indent)。在 K&R 模式下调用,它将按照您的要求缩进您的示例代码(假设您确实想要 puts缩进):

$ indent -kr <sample.c
int main()
{
    puts("Hello world");
}
Run Code Online (Sandbox Code Playgroud)

更现代的解决方案可能是clang-format ( http://clang.llvm.org/docs/ClangFormat.html ),它可以根据样式文件以多种方式进行配置。