Getting Started
This page explains how to get started with packages related to clang-format
and highlights their common features.
For more detailed information, please refer to each package's individual documentation.
Installation
clang-format-node
clang-format-node
See the Installation section of clang-format-node
.
clang-format-git
clang-format-git
See the Installation section of clang-format-git
.
clang-format-git-python
clang-format-git-python
See the Installation section of clang-format-git-python
.
Usage: clang-format
clang-format
This feature is included in the clang-format-node
package.
If you want to learn more about clang-format
itself, see the clang-format style options
.
[!TIP]
clang-format
can take multiple files as arguments.
Basic
Global
Local
Use
npx
to run a locally installed package.
Frequently used commands
--version
: Check the version ofclang-format
.Output example
https://github.com/llvm/llvm-project
: The Git repository URL for the LLVM project, which includes Clang.3b5b5c1ec4a3095ab096dd780e84d7ab81f3d7ff
: The commit hash for the specific version used to buildclang-format
, allowing you to trace the source code exactly.
--help
: Help view additional options.--dry-run
or-n
: Makes an WARNING whenexample.cpp
is not correctly formatted.--dry-run
and-n
options are equivalent.-Werror --dry-run
or-Werror -n
: Makes an ERROR whenexample.cpp
is not correctly formatted.Similar to
eslint
orprettier --check
commands.--dry-run
and-n
options are equivalent.-i
: Automatically fix unformatted files.Similar to
eslint --fix
orprettier --write
commands.
Glob patterns
Unfortunately, there is no way to apply clang-format
recursively. *.cpp
will only match files in the current directory, not subdirectories. Even **/*
doesn't work.
So, you need to use the find
command in POSIX. If you are a Windows user, use git bash. then you can use the find
command. The find
command recursively searches through directories.
It is simple but can produce an error if the Argument list is too long. In that case, use xargs
Basic
To recursively search for all
.cpp
files in the current directory, use:If the argument list is too long, use
xargs
. And if file names contain spaces or special characters, use-print0
and-0
options.-print0
makesfind
output file names separated by null characters (\0
), and-0
tellsxargs
to correctly handle these null-separated file names.With regular expressions
To recursively search for all
.cpp
and.h
files in the current directory using a regular expression, use:With negation patterns
To exclude
excluded_file.cpp
from the.cpp
files, use:
Usage: git-clang-format
git-clang-format
This feature is included in the clang-format-git
and clang-format-git-python
package.
clang-format-git
and clang-format-git-python
are two options for using git-clang-format
, so you can choose the one that best fits your setup. The usage is same to 'angular/clang-format'.
Using Without the Python3 Dependency
This package provides a standalone executable version of git-clang-format
, so you won’t need to install Python3. But it's size is quite large.
Usage
See the Usage section of clang-format-git
.
Using with Python3 Dependency
This version has a smaller file size than clang-format-git
, but it does require Python3 to run.
Usage
See the Usage section of clang-format-git-python
.
How to use
Basic
Global
Local
Use
npx
to run a locally installed package.
Frequently used commands
--help
: Help view additional options.
APIs
Each package also supports JSDoc type hints with the following APIs, so you’ll see more detailed information directly in your code editor.
clang-format-node
clang-format-node
See the APIs section of clang-format-node
.
clang-format-git
clang-format-git
See the APIs section of clang-format-git
.
clang-format-git-python
clang-format-git-python
See the APIs section of clang-format-git-python
.
You can create .clang-format-ignore
files to make clang-format
ignore certain files. A .clang-format-ignore
file consists of patterns of file path names. It has the following format:
A blank line is skipped.
Leading and trailing spaces of a line are trimmed.
A line starting with a hash (
#
) is a comment.A non-comment line is a single pattern.
The slash (
/
) is used as the directory separator.A pattern is relative to the directory of the
.clang-format-ignore
file (or the root directory if the pattern starts with a slash). Patterns containing drive names (e.g.C:
) are not supported.Patterns follow the rules specified in POSIX 2.13.1, 2.13.2, and Rule 1 of 2.13.3.
A pattern is negated if it starts with a bang (
!
).
To match all files in a directory, use e.g. foo/bar/*
. To match all files in the directory of the .clang-format-ignore
file, use *
. Multiple .clang-format-ignore
files are supported similar to the .clang-format
files, with a lower directory level file voiding the higher level ones.
Use with husky
and lint-staged
husky
and lint-staged
Ensuring that changes to your code are properly formatted is an important part of your development workflow. Use husky
and lint-staged
for your continuous integration process.
husky
(v8.x)
husky
(v8.x)lint-staged
(v15.x)
lint-staged
(v15.x)Check
Fix
[!TIP]
If
example1.cpp
andexample2.c
are staged, thennpx clang-format -Werror -n example1.cpp example2.c
will be excuted.
Last updated