LinuxCommandLibrary

astyle

Format source code automatically

TLDR

Apply the default style of 4 spaces per indent and no formatting changes

$ astyle [source_file]
copy

Apply the Java style with attached braces
$ astyle [[-A2|--style=java]] [path/to/file]
copy

Apply the allman style with broken braces
$ astyle [[-A1|--style=allman]] [path/to/file]
copy

Apply a custom indent using spaces. Choose between 2 and 20 spaces
$ astyle [[-s|--indent=spaces=]][number_of_spaces] [path/to/file]
copy

Apply a custom indent using tabs. Choose between 2 and 20 tabs
$ astyle [[-t|--indent=tab=]][number_of_tabs] [path/to/file]
copy

SYNOPSIS

astyle [options] [files...] | --version | --help

PARAMETERS

--mode=java|c|c++|csharp
    Set language mode (default auto-detect)

--style=allman|ansi|bsd|gnu|java|kr|linux|webkit|stroustrup|mozilla|python|ratliff|whitesmith|banner|gnome
    Select predefined style

--indent=spaces|tab|force-tab|force-tab-x[=num]
    Indentation style and size (default spaces=4)

--indent-single-line
    Indent one-line blocks

--indent-classes
    Indent class blocks

--indent-switches
    Indent switch blocks

--indent-namespaces
    Indent namespace blocks

--indent-labels
    Indent labels

--indent-preprocessor
    Indent preprocessor blocks

--indent-col1-comments
    Indent column 1 comments

--min-conditional-indent=num
    Minimum conditional indent (0-2, default 0)

--pad-oper
    Pad operators with spaces

--pad-paren
    Pad parentheses

--pad-paren-out
    Pad outer parentheses

--pad-paren-in
    Pad inner parentheses

--pad-header
    Pad header comments

--pad-comma
    Pad commas

--align-pointer=name|middle|type
    Align pointer stars

--align-reference=name|middle|type
    Align reference ampersands

--break-blocks
    Insert line breaks before blocks

--break-elseifs
    Separate else-if into new line

--break-closing-brackets
    Break after closing brackets

--break-chained-methods
    Break chained methods

--break-one-line-headers
    Break one-line headers

--keep-one-line-statements
    Keep one-line statements

--convert-tabs
    Convert tabs to spaces

--max-code-length=num
    Maximum line length

--max-instatement-indent=num
    Maximum in-statement indent

--shorten-else-if
    Use 'else if' instead of 'else { if'

--fill-empty-lines
    Fill empty lines with spaces

--lineend=windows|linux|mac
    Set line endings

--suffix=ext
    Add suffix to formatted files (default _astyle)

--exclude=dir|pattern
    Exclude directories or patterns

--recursive
    Process directories recursively

--dry-run
    Show changes without modifying

--formatted
    List formatted files

--quiet
    Suppress output

--errors-to-stdout
    Send errors to stdout

--html
    Produce HTML output

--stdout
    Output to stdout

--options=file
    Read options from file

DESCRIPTION

Astyle (Artistic Style) is a fast, configurable source code formatter for C, C++, C#, and Java. It enforces consistent style by adjusting indentation, brace placement, spacing, and line breaks.

Predefined styles include Allman (opening braces on new lines), ANSI/K&R (braces on same line), BSD, GNU, Java, KR, Linux, WebKit, and others. Custom options control tabs vs. spaces, padding around operators/parentheses, conditional indentation, pointer/reference alignment, and more. It supports recursive processing of directories, file suffixes for selection, and output to stdout or HTML.

Options can be specified on the command line, in a <filename>.astylerc file, or ~/.astylerc. Ideal for projects requiring uniform code appearance, it preserves code logic while improving readability. Processes files in-place or to new files with --suffix option.

CAVEATS

Does not modify original files unless specified; backups not automatic. May alter code appearance unexpectedly with aggressive options. Limited support for very complex macros or templates.

CONFIGURATION FILES

Options from ~/.astylerc, <project>/.astylerc, or --options=<file> override command line.

EXAMPLES

astyle --style=linux --recursive *.cpp
astyle --options=style.cfg src/

HISTORY

Developed by Tal Davidson starting 1998. Initial C++ focus, added C# and Java later. Current v3.1 (2020+) supports modern C++ features. Widely used in open-source for consistent styling.

SEE ALSO

Copied to clipboard