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/source.c] [path/to/another_source.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] [FILE...]
indent [OPTIONS] [INPUT_FILE] [-o OUTPUT_FILE]

PARAMETERS

-iN, --indent-level=N
    Sets the indentation level to N spaces (default is 8).

-tsN, --tab-size=N
    Specifies the size of tabs in spaces (default is 8).

-nut, --no-tabs
    Replaces all tabs with an equivalent number of spaces.

-lN, --line-length=N
    Sets the maximum line length to N characters (default is 78).

-kr, --k-and-r
    Uses the Kernighan & Ritchie (K&R) style for formatting.

-gnu, --gnu
    Uses the GNU coding style.

-orig, --original-k-and-r
    Uses the original K&R style (less strict than -kr).

-br, --braces-on-if-line
    Puts opening braces for `if`, `for`, `while`, etc., on the same line.

-bl, --braces-on-func-line
    Puts opening braces for function definitions on the line after the function header.

-cs, --cuddle-else
    Cuddles `else` with the preceding `}`.

-o FILE, --output=FILE
    Writes output to FILE instead of modifying the input file in place.

-st, --standard-output
    Writes output to standard output, without modifying input files.

-v, --version
    Prints the version number of indent and exits.

-bad, --blank-lines-after-declarations
    Forces a blank line after local variable declarations.

-bap, --blank-lines-after-parameters
    Forces a blank line after parameter declarations.

DESCRIPTION

indent is a command-line utility used to reformat C, C++, and Objective-C source code to a consistent style. Its primary purpose is to improve code readability and enforce coding standards across projects or individual files. indent offers extensive customization options, allowing users to control various aspects of code formatting, including indentation levels, brace placement, blank line usage, comment formatting, and more. It can apply predefined styles like GNU, K&R (Kernighan & Ritchie), and original K&R, or a user-defined style through a multitude of switches. While it's a classic tool, commonly found on Unix-like systems, it remains a powerful option for maintaining code uniformity, especially in projects where specific coding conventions are crucial. indent can process files in place or write to standard output/a specified output file.

CAVEATS

indent is primarily designed for C code and may have limited or imperfect support for more complex C++ features or Objective-C syntax. It can sometimes produce unexpected formatting for intricate preprocessor directives or highly unusual code structures. While powerful, users should review the output, especially for critical projects, as the tool applies stylistic changes rather than semantic ones. Modern IDEs often provide integrated, language-aware formatters that might be more convenient for contemporary development workflows.

<I>CONFIGURATION FILE</I>

indent can read default options from a personal configuration file named .indent.pro in the user's home directory or the current directory. Options specified on the command line override those in the configuration file. This allows users or projects to define custom default formatting styles without explicitly listing all options every time.

<I>STYLISTIC PRESETS</I>

Beyond explicit options, indent provides predefined style presets like -gnu (GNU style), -kr (Kernighan & Ritchie style), and -orig (Original K&R style). These presets automatically configure a large number of indent options to conform to widely recognized coding conventions, offering a quick way to apply a consistent style without mastering every individual flag.

HISTORY

indent is one of the oldest and most enduring utilities for code formatting on Unix-like systems. It originated in the early days of C programming, designed to enforce consistent coding styles, particularly the Kernighan & Ritchie (K&R) style. Over decades, it has evolved to support additional conventions, most notably the GNU coding style, which is often its default. Its widespread adoption led to its inclusion in GNU Emacs and its availability as a standard tool on most Linux distributions, solidifying its place as a classic Unix utility for code beautification and standardization.

SEE ALSO

astyle(1), clang-format, gfmt(1)

Copied to clipboard