indent
Reformat C source code according to configurable style rules
By CMD Script Team · 5 min read · Last updated
indent [OPTIONS] FILE...Options
| Flag | Description |
|---|---|
-gnu | Format using GNU style (the default), with braces on their own line and specific indentation conventions |
-kr | Format using Kernighan & Ritchie (K&R) style |
-bsd / -orig | Format using the original Berkeley/BSD indentation style |
-i N | Set indentation width to N spaces |
-o FILE | Write reformatted output to FILE instead of modifying the input in place |
-nbad | Do not force a blank line after every block of declarations |
-bad | Force a blank line after every block of declarations |
Distribution compatibility
- Ubuntu
- Debian
- Fedora
- Arch
- macOS (via Homebrew; not preinstalled)
What it does
indent reformats C source code according to a configurable style — controlling
indentation width, brace placement, spacing around operators, and comment alignment —
without changing the program's behavior. It supports several named style presets (GNU,
K&R, original Berkeley/BSD) selectable with a single flag, plus many fine-grained
options for customizing exact formatting rules. It's one of the older members of the C
formatting toolchain; for many projects today, especially those mixing C and C++, the
more actively developed clang-format has become the default choice, though indent
remains packaged and functional on current distributions for straightforward C
formatting needs.
Beginner examples
indent myfile.c— reformat a file in place using the default GNU styleindent -kr myfile.c— reformat using Kernighan & Ritchie styleindent -bsd myfile.c— reformat using original Berkeley/BSD styleindent myfile.c -o myfile.formatted.c— write the result to a new file instead of overwriting the original
indent -kr -i4 myfile.c
Advanced examples
- Reformat an entire project's C files in place:
find . -name "*.c" -exec indent {} \; - Combine explicit style flags for fine-grained control beyond a named preset:
indent -kr -i4 -nut -br myfile.c(K&R style, 4-space indent, spaces not tabs, braces on same line as control statements) - Store team-wide formatting rules in a
.indent.profile in the project root so every invocation ofindentin that directory picks up the same settings automatically. - Preview changes before overwriting by diffing:
indent myfile.c -o /tmp/preview.c && diff myfile.c /tmp/preview.c
find src/ -name "*.c" -o -name "*.h" | xargs indent -kr -i4
Common mistakes
- Running
indentin place on a file with uncommitted, unreviewed changes and losing track of what the tool actually altered — always run it on a clean git working tree so the diff is easy to review afterward. - Mixing
indentandclang-formaton the same codebase without agreeing on one as the canonical formatter, causing files to bounce between two slightly different styles every time a different contributor formats them. - Assuming
indent's named styles (GNU, K&R, BSD) match a team's exact preferred style out of the box — they're close starting points, not guarantees; fine-tune with additional flags or a.indent.profile. - Forgetting
indentis C-oriented; running it directly on C++ files can mishandle constructs like templates or namespaces that didn't exist when the tool was designed.
Tips
- Commit an
.indent.profile to the repository root so every contributor'sindentinvocation uses the same style automatically. - Run
indenton a clean git working tree so you can review its changes as an isolated diff before committing. - For mixed C/C++ codebases, consider standardizing on
clang-formatinstead, sinceindentis not designed with C++-specific syntax in mind. - Use
-oto preview reformatting into a separate file before committing to modifying files in place.
Best practices
- Pick one formatter for a given codebase — either
indentorclang-format— and apply it consistently via a pre-commit hook or CI check, rather than letting contributors use whichever tool they personally prefer. - Check in a shared style configuration (
.indent.pro) so formatting is deterministic and doesn't depend on each developer's local defaults. - Run
indent(or any reformatter) as an isolated commit separate from logic changes, so reviewers can distinguish formatting-only diffs from functional ones. - For new C++ projects, prefer
clang-format, which has broader language support and more active maintenance thanindent.
Try it yourself
A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.
Real-world use cases
- Normalizing formatting across an older C codebase that predates any enforced style guide.
- Enforcing a consistent style in a project that intentionally favors GNU or K&R
conventions, where
indent's named presets map directly to the desired style. - Cleaning up whitespace and brace-placement inconsistencies before a code review, so reviewers can focus on logic instead of style nits.
- Preparing legacy vendor C source for inclusion in a project with a house style, by
running it through
indentwith the project's chosen preset.
Common interview questions
- What does indent actually change in source code? Only whitespace, indentation, brace placement, and spacing conventions — it doesn't alter the program's parsed meaning or behavior.
- How does indent compare to clang-format? indent is older and C-specific, with named style presets and command-line flags; clang-format is newer, supports both C and C++, uses a more flexible YAML configuration, and has wider adoption in modern projects, though indent remains available and functional for basic C formatting.
- How would you enforce a consistent style across a team using indent? Commit a
shared
.indent.prooptions file to the repository so every invocation picks up the same settings, and run it via a pre-commit hook or CI check rather than relying on manual, ad hoc formatting. - Why might you avoid running indent directly on C++ code? Because it was designed for C and may not correctly handle C++-specific syntax like templates, namespaces, or operator overloading, potentially producing incorrect or ugly formatting.
Frequently Asked Questions
How does indent differ from clang-format?
indent is an older, C-focused formatter with a fixed set of named styles (GNU, K&R, BSD/Allman, etc.) configured via command-line flags. clang-format is newer, supports both C and C++ (and more), uses a YAML-based style configuration, and has broader adoption and active maintenance, making it the more common choice for modern projects — but indent is still packaged and usable for straightforward C formatting.
Does indent change program behavior?
No. indent only rewrites whitespace, indentation, and brace placement; it doesn't alter the parsed meaning of the code. It's designed to be behavior-preserving, though it's always good practice to review a diff after running it, especially on a codebase you don't fully own.
How do I apply indent to an entire project consistently?
Create a project-wide options file (traditionally named .indent.pro in the working directory or referenced via INDENT_PROFILE) with your chosen style flags, then run indent on each file, or use a wrapper script/find loop such as find . -name '*.c' -exec indent {} \;.
Cheat sheet
Download a quick-reference cheat sheet for indent.