astyle
Format source code automatically
TLDR
Apply the default style of 4 spaces per indent and no formatting changes
Apply the Java style with attached braces
Apply the allman style with broken braces
Apply a custom indent using spaces. Choose between 2 and 20 spaces
Apply a custom indent using tabs. Choose between 2 and 20 tabs
SYNOPSIS
astyle [options] [file ...]
astyle [options] < input_file > output_file
PARAMETERS
--style={style}
Sets the indentation style. Common styles include allman, kr, gnu, java, attach, etc.
--indent={type}
Specifies indentation type and size, e.g., spaces=4, tab=4, force-tab=4.
--brackets={type}
Controls bracket placement, e.g., allman, attach, linux, stroustrup, bsd.
--pad-oper
Adds space padding around operators (e.g., a = b instead of a=b).
--unpad-paren
Removes space padding around parentheses (e.g., (a) instead of ( a )).
--add-brackets
Adds brackets to all if, for, while, and do statements.
--suffix={suffix}
Sets the backup file suffix (default: .orig). Use --suffix=none to disable backups.
--dry-run
Do not modify files; only print formatted output to the console.
--formatted
Prints formatted files to console, but does not modify the original files.
--recursive
Recursively processes files in subdirectories.
--exclude={name}
Excludes files or directories matching the specified name.
--verbose
Prints progress messages to the console.
--version
Prints version information and exits.
--help
Displays a help message with all available options.
DESCRIPTION
astyle (Artistic Style) is an open-source, fast, and configurable source code formatter for C, C++, C#, and Java. It automatically reformats source code files according to a user-defined style, ensuring consistency and readability across projects.
It supports various indentation styles (e.g., K&R, Allman, GNU, Whitesmith, Pico) and offers extensive options for controlling bracket placement, padding around operators and parentheses, line breaks, and other stylistic elements. astyle can process single files, multiple files, or recursively format entire directories. It's often used in development workflows to maintain coding standards. By default, it creates backup files with a .orig extension before modifying the original.
CAVEATS
astyle modifies files in place by default, creating .orig backups. Always back up your code or use --dry-run or --formatted first, especially in production environments.
It's a stylistic formatter and will not fix syntax errors or logical issues in your code. While highly configurable, achieving very specific or unusual formatting styles might require intricate option combinations, and it may not perfectly handle all edge cases or highly unconventional code constructs.
CONFIGURATION FILES
astyle can read options from a configuration file, typically named .astylerc. This file can be located in the user's home directory or the current working directory, allowing users to define persistent style settings without typing long command-line arguments every time.
PIPES AND STANDARD I/O
astyle can be effectively used with pipes, taking source code input from stdin and sending the formatted output to stdout. This capability makes it very versatile for integration into scripts, IDEs, or automated build systems.
HISTORY
Artistic Style (astyle) was initially created by Tal Davidson and first released around 2000. Its development was driven by the need for a highly configurable and cross-platform code formatter for C, C++, and Java. It quickly gained popularity due to its flexibility and the ability to enforce consistent coding styles across various projects and development teams. It has been continuously maintained and updated by a community of contributors over the years, adding support for new languages like C# and expanding its array of formatting options.
SEE ALSO
clang-format(1), indent(1)