IntelliJ IDEA 2021.1 Help

Format files from the command line

IntelliJ IDEA can format your code according to the configured code style settings. You can also apply your code style formatting to the specified files from the command line.

The command-line formatter launches an instance of IntelliJ IDEA in the background and applies the formatting. It will not work if another instance of IntelliJ IDEA is already running. In this case, you can perform code style formatting from the running instance. Use the command-line formatter for automated regular maintenance of a large codebase with many contributors to ensure a consistent coding style.

To be able to format files, install and enable plugins with support for the corresponding file types in IntelliJ IDEA (for example, the Shell Script plugin to format shell script files).

You can find the executable for running IntelliJ IDEA in the installation directory under bin. To use this executable as the command-line launcher, add it to your system PATH as described in Command-line interface.

Syntax
idea64.exe format [<options>] <path ...>
Examples

Format two specific files from the C:\Data\src directory using the default code style settings:

idea64.exe format C:\Data\src\hello.html C:\Data\src\world.html

Recursively format all files in the C:\Data\src directory including all subdirectories using the default code style settings:

idea64.exe format -r C:\Data\src

Non-recursively format all the .xml and .html files in the C:\Data\src directory using code style settings from C:\Data\settings.xml:

idea64.exe format -s C:\Data\settings.xml -m *.xml,*.html C:\Data\src

IntelliJ IDEA includes a script for running the command-line code formatter. By default, it is located in the application package: IntelliJ IDEA.app/Contents/bin/format.sh

Syntax
format.sh [<options>] <path ...>
Examples

Format two specific files from the ~/Data/src directory using the default code style settings:

format.sh ~/Data/src/hello.html ~/Data/src/world.html

Recursively format all files in the ~/Data/src directory including all subdirectories using the default code style settings:

format.sh -r ~/Data/src

Non-recursively format all the .xml and .html files in the ~/Data/src directory using code style settings from ~/Data/settings.xml:

format.sh -s ~/Data/settings.xml -m *.xml,*.html ~/Data/src

You can find the script for running IntelliJ IDEA in the installation directory under bin. To use this script as the command-line launcher, add it to your system PATH as described in Command-line interface.

Syntax
idea64.exe format [<options>] <path ...>
Examples

Format two specific files from the ~/Data/src directory using the default code style settings:

idea.sh format ~/Data/src/hello.html ~/Data/src/world.html

Recursively format all files in the ~/Data/src directory including all subdirectories using the default code style settings:

idea.sh format -r ~/Data/src

Non-recursively format all the .xml and .html files in the ~/Data/src directory using code style settings from ~/Data/settings.xml:

idea.sh format -s ~/Data/settings.xml -m *.xml,*.html ~/Data/src

Options

OptionDescription
-hShow the help message and quit.
-m|-maskSpecify a comma-separated list of file masks that define the files to be processed. You can use the * (any string) and ? (any single character) wildcards.
-r|-RProcess specified directories recursively.
-s|-settings

Specify the code style settings file to use for formatting. This can be one of the following:

  • A file with the exported code style settings: open the Editor | Code Style page of the IDE settings Ctrl+Alt+S, click The Show Scheme Actions button, and select Export.

  • The .idea/codeStyleSettings.xml file stored in your project directory (for IntelliJ IDEA version 2017.2 and earlier).

  • The .idea/codeStyles/Project.xml file stored in your project directory (for IntelliJ IDEA version 2017.3 and later).

The formatter also looks for .editorconfig files in the parent directories, and you can explicitly use EditorConfig for formatting instead of the IntelliJ IDEA code style settings. For more information, see Manage code style on a directory level with EditorConfig.

Last modified: 19 May 2021