function::indent - Linux


Overview

indent is a text processing tool that reformats C code to follow a consistent indentation style. It can automatically indent code blocks, aligning statements, comments, and preprocessor directives for improved readability and maintainability.

Syntax

indent [options] [input_file]

Options/Flags

| Option | Description |
|—|—|
| -a | Always indent function arguments |
| -b | Indent braces from the beginning of the line |
| -c | Continuation lines are indented if they are at the same level of the current line |
| -d | Create a non-indented version of the input (DEPRECATED) |
| -e | Replace existing whitespace with the calculated indentation |
| -i | Automatically indent variable names |
| -l | Line comments are indented with the enclosing block |
| -nbc | Do not indent preprocessor lines that start with a non-blank character |
| -nfca | Do not format case labels |
| -ncps | Do not format comments after preprocessor directives |
| -ncs | Do not format comments at the start of lines |
| -p | Prefix each output line with a percentage sign (%) |
| -pp | Use "pretty printing" to improve the appearance of the output |
| -prs | Prefix comments with "#" |
| -T | Use tabs instead of spaces for indentation |
| -t | Specifies the number of characters to indent each nesting level |

Examples

To indent a C source file named source.c:

indent source.c

To automatically indent function arguments and align braces:

indent -ai source.c

To use spaces for indentation instead of tabs:

indent -T source.c

Common Issues

Error: Indentation level too high
Solution: Increase the value of the -t option to allow deeper indentation levels.

Error: Malformed input
Solution: Check the input code for syntax errors or unexpected characters that may be causing the issue.

Integration

Using indent with a text editor:
Many text editors can be configured to automatically format code using indent on save or specific keystrokes.

Creating a custom indenting script:
Advanced users can create shell scripts or Vim macros that utilize indent to automate code formatting for specific projects or coding styles.

Related Commands

  • clang-format
  • astyle
  • gofmt