Development
indent
Format C and C++ source code with consistent indentation.
Additional Notes
indent reformats C and C++ source code according to configurable indentation and formatting rules. It adjusts indentation, line breaks, spacing, brace placement, comment formatting, and other code layout aspects to match a specified style.
It supports several built-in coding styles (Kernighan & Ritchie, GNU, Berkeley, Linux kernel) and allows fine-grained control over virtually every formatting detail through command-line options. It is useful for enforcing consistent code style across a project, cleaning up poorly formatted code, or batch-formatting many files at once.
Syntax
indent [options] file...
Parameters
file: One or more C or C++ source files to format. Files are modified in place unless backup options are specified.
Common Style Options
-kr: Use Kernighan & Ritchie style.-gnu: Use GNU style (default).-orig: Use Berkeley style.-linux: Use Linux kernel style.-bap: Blank line after procedure bodies.-bad: Blank line after declarations.-bap: Blank line after function bodies.-bbb: Blank line before block comments.-nbad: Do not force blank lines after declarations.-bbo: Prefer break before boolean operators.-bc: Putelseon new line after closing brace.-bl: Put opening braces on new lines.-bli n: Indent braces bynspaces.-bls: Put braces of struct/union/enum on new lines.-br: Put opening braces on the same line as the statement.-bs: Prefer a space betweensizeofand its argument.-c n: Comment indentation column.-cd n: Declaration comment column.-cdb: Comment delimiters on blank lines.-ce: Cuddleelsewith preceding}.-ci n: Continuation indent ofnspaces.-cli n: Case label indent ofnspaces.-d n: Set indentation for comments not to the right of code.-di n: Put variables in columns.-fc1: Format comments in first column.-fca: Format all comments.-hnl: Prefer to break long lines at newlines.-i n: Set indentation level tonspaces (default is 2).-ip n: Indent parameter types in old-style function definitions bynspaces.-l n: Set maximum line length toncolumns (default is 78).-lc n: Set maximum comment line length.-lp: Align continued line parameter lists.-npsl: Put return type on the same line as function name.-psl: Put return type on its own line before function name.-sc: Put*/at the right of comments.-sob: Swallow optional blank lines.-st: Write output to stdout instead of modifying files in place.-T typename: Addtypenameto the type recognition list.-ts n: Set tab size toncolumns.-ut: Use tabs for indentation.-nut: Use spaces for indentation (no tabs).-v: Verbose mode.
Examples
indent -kr file.c
Format file.c in Kernighan & Ritchie style.
indent -linux -i8 file.c
Format using Linux kernel style with 8-space indentation.
indent -st -br -ce file.c > formatted.c
Format file.c with braces on the same line and else cuddled, writing to stdout.
indent -gnu -l120 -nut *.c
Format all C files in the current directory with GNU style, 120-character line length, and spaces instead of tabs.
indent -kr -bad -bap -i4 -br -ce -nut source.c
Apply a custom style based on K&R but with 4-space indentation and no tabs.
Practical Notes
indentmodifies files in place by default. Use-stto send output to stdout and inspect changes first.- Always back up your source files (
-bflag creates a backup with~suffix) before batch-formatting. - Not all code formats perfectly;
indentmay struggle with preprocessor macros,#ifdefblocks, or unusual constructs. - For large projects, use
indentwith version control (e.g.,git diffafterward) to review all changes. indentonly handles C and C++. For other languages, use language-specific formatters.- The tool is most useful when applied consistently to an entire codebase.