IntelliJ IDEA 2023.1 Help

Scalafmt

IntelliJ IDEA lets you integrate with Scalafmt formatter.

Configure Scalafmt

You can enable Scala formatter in your project and configure the formatter settings.

  1. In the Settings dialog (Control+Alt+S), go to Editor | Code Style | Scala.

  2. Switch the option in the Formatter field to Scalafmt. You will see that the list of tabs below the formatter field is changed.

    Scalafmt settings

    Check the following notable Scalafmt formatter options:

    • Use IntelliJ formatter for code range formatting: this option is selected by default. When you reformat a code selection in the editor, it will be reformatted with the default IntelliJ IDEA formatter. The code selection formatting is also applied to code snippets generated with various actions and live templates. The Scalafmt formatter can only reformat the entire file.

    • Reformat on file save: if you select this option, IntelliJ IDEA reformats a file when you save it Control+S, switch between the active editor and tool windows, or execute a run/debug configuration.

    • Configuration: if IntelliJ IDEA does not find the .scalafmt.conf file with the specified formatter version in your project, it uses the current default Scalafmt version (1.5.1). You can override the default version if you need.

Override the Scalafmt version

  1. Create the .scalafmt.conf file in the root of your project. Right-click your project and select New | File.

    Scalafmt configuration file
  2. Open the configuration file in the editor and specify the Scalafmt version you want to use along with other formatting options.

  3. Access the Scala formatter settings. IntelliJ IDEA reflects your changes for Scalafmt under the Configuration option.

    Scalafmt settings with changed version

Reformat Scala code in the editor

  1. In the editor, select code that you want to reformat. The code selection is reformatted by IntelliJ IDEA since the Scalafmt formatter can be applied to the entire file only. If you do not make any selection, IntelliJ IDEA will reformat the whole file using Scalafmt.

  2. From the main menu, select Code | Reformat Control+Alt+L.

    If you need to add additional options before reformatting, from the main menu, select Code | Reformat File Control+Alt+Shift+L and in the Reformat File dialog make the required settings.

    Reformat file dialog

    For more information on reformatting code, refer to Reformat code.

Last modified: 31 January 2023