← 返回命令列表

Linux command

clang-format 命令

文件

复制后可按需替换文件名、目录或参数。

常用示例

Format file and output to stdout

clang-format [file.cpp]

Format file in place

clang-format -i [file.cpp]

Format with specific style

clang-format --style=[Google] [file.cpp]

Format using project config file

clang-format --style=file [file.cpp]

Format code from stdin

echo "int main(){}" | clang-format

Generate style config file

clang-format --style=[LLVM] --dump-config > [.clang-format]

Format specific lines only

clang-format --lines=[10:20] [file.cpp]

Check if file is formatted

clang-format --dry-run --Werror [file.cpp]

说明

clang-format is an automatic code formatting tool for C, C++, Java, JavaScript, JSON, Objective-C, Protobuf, and C# source files. It is part of the LLVM/Clang project and formats code according to configurable style rules. The tool ships with several predefined styles (LLVM, Google, Chromium, Mozilla, WebKit, Microsoft, GNU) and supports extensive customization through .clang-format configuration files. When using --style=file, it searches parent directories for the nearest configuration file. clang-format integrates with most major editors and CI/CD pipelines. The --dry-run option with --Werror enables enforcement of formatting standards in automated checks without modifying files.

参数

-i
Edit files in place
--style=_style_
Coding style (LLVM, Google, Chromium, Mozilla, WebKit, Microsoft, file)
--dump-config
Dump configuration to stdout
--assume-filename=_name_
Filename for language detection when reading stdin
--lines=_start:end_
Format only specified line range
--dry-run
Don't write changes, exit with error if changes needed
--Werror
Treat formatting issues as errors
--fallback-style=_style_
Style when no .clang-format found
--verbose
Show files being processed

FAQ

What is the clang-format command used for?

clang-format is an automatic code formatting tool for C, C++, Java, JavaScript, JSON, Objective-C, Protobuf, and C# source files. It is part of the LLVM/Clang project and formats code according to configurable style rules. The tool ships with several predefined styles (LLVM, Google, Chromium, Mozilla, WebKit, Microsoft, GNU) and supports extensive customization through .clang-format configuration files. When using --style=file, it searches parent directories for the nearest configuration file. clang-format integrates with most major editors and CI/CD pipelines. The --dry-run option with --Werror enables enforcement of formatting standards in automated checks without modifying files.

How do I run a basic clang-format example?

Run `clang-format [file.cpp]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -i do in clang-format?

Edit files in place