LinuxCommandLibrary

indent

Format and beautify C code

TLDR

Format C/C++ source according to the Linux style guide, automatically back up the original files, and replace with the indented versions

$ indent [[-linux|--linux-style]] [path/to/source1.c path/to/source2.c ...]
copy

Format C/C++ source according to the GNU style, saving the indented version to a different file
$ indent [[-gnu|--gnu-style]] [path/to/source.c] -o [path/to/indented_source.c]
copy

Format C/C++ source according to the style of Kernighan & Ritchie (K&R), no tabs, 3 spaces per indent, and wrap lines at 120 characters
$ indent [[-kr|--k-and-r-style]] [[-il|--indent-level]]3 [[-nut|--no-tabs]] [[-l|--line-length]]120 [path/to/source.c] -o [path/to/indented_source.c]
copy

SYNOPSIS

indent [options] [inputfile] [outputfile]

PARAMETERS

-bap
    Put braces on line after if/for/while if it fits.

-nbap
    Opposite of -bap; braces on same line.

-bacc
    Put braces on line after function declaration args if they fit.

-nbacc
    Opposite of -bacc.

-br
    Use K&R (right) brace style.

-bl
    Use BSD/ANSI (left) brace style.

-brs
    No space after function name before (.

-nprs
    Opposite; space after function name.

-c
    Set space after case label to <n> (default 1).

-cdw
    Use cuddled else style.

-nce
    No cuddled else.

-ci
    Indentation after case labels: <n> spaces.

-cp
    Indentation after case: <n> columns.

-cli
    Line length for function header indentation: <n>.

-d
    Decl indent relative to first decl: <n>.

-di
    Decl indent after parens: <n>.

-ncs
    No space after cast operator.

-cs
    Space after cast operator.

-dj
    Space after justification keyword.

-nfc1
    No space after unclosed (.

-fc1
    Space after unclosed (.

-et
    Use tabs for output.

-nut
    Use spaces instead of tabs.

-i
    Indentation level: <n> spaces (default 8).

-ip
    Parameter continuation indentation: <n>.

-l
    Line length: <n> (default 78).

-lp
    Indent labels backward from code.

-npro
    No space before return type.

-npcs
    No space after procedure name.

-orig
    Use original file's indentation style.

-st
    Standard output (stdout).

-T
    Declare symbol as a type-macro.

-v
    Verbose mode.

--version
    Print version info.

--help
    Show help.

DESCRIPTION

The indent command is a powerful utility for reformatting C and C++ source code to enhance readability and enforce consistent coding styles. It automatically adjusts indentation, positions braces, aligns declarations and statements, and handles continuation lines according to user-specified options or predefined styles such as K&R, BSD, or GNU.

Indent processes input from files or standard input, writing formatted output to standard output or a specified file. It supports customization for tab width, indentation levels, brace placement (e.g., on separate lines or same line), and spacing around operators and keywords. This makes it invaluable for maintaining code standards in large projects, preparing code for printing, or converting between styles.

While primarily for C, it handles much of C++ and some other languages with C-like syntax. Options allow precise control, but improper use can alter code logic if preprocessor directives are mishandled. Always test on backups.

CAVEATS

May misformat complex macros or modern C++ features. Always backup source files before processing. Does not parse preprocessor directives perfectly; use -npro cautiously.

STANDARD STYLES

Use -kr for K&R style, -orig to mimic input style, or -linux/-gnu for kernel/GNU conventions.

EXAMPLE USAGE

indent -kr input.c reformats to K&R style.
indent -st -i4 *.c processes all C files with 4-space indents to stdout.

HISTORY

Developed in 1976 by David Willcox at Bell Labs for K&R C. Ported to Unix; GNU version started 1989 by Jim Kingdon. Maintained as part of GNU (version 2.2.13 in 2023). Widely used in Unix/Linux for decades.

SEE ALSO

Copied to clipboard