How to apply .editorconfig to an existing project in IntelliJ IDEA

24k views Asked by At

I have created a .editorconfig file as shown below for IntelliJ IDEA.

# editorconfig.org

root = true

[*]
charset = utf-8
insert_final_newline = true
trim_trailing_whitespace = true
end_of_line = lf
indent_style = space
indent_size = 4

[*.js]
indent_size = 4

[{package.json}]
indent_style = space
indent_size = 4

How do I apply this code style to all the files in one go? Right now, I will open a file and press Ctrl + Alt + l which will do the job for that file, but is there a way to apply this as the configuration for all the application in IntelliJ IDEA 14?

3

There are 3 answers

1
Michal Miky Jankovský On BEST ANSWER

Just enable it... ;)

(Unfortunately the setting is off by default.)

Menu SettingsEditorCode Style.

Checkbox Enable EditorConfig support

Enter image description here

1
T04435 On

If you added the file after project initialization.

You need to add the file .editorconfig on the root of the project.

Once happy with your file configuration, right-click and select synchronize '.editorconfig'.

2
Sumurai8 On

Make sure to follow the steps in Michal's answer first. If you want to retroactively apply this editorconfig to your existing files, do the following:

  • I recommend committing all your files to Git, so you can reset to that commit if things did not go as planned.
  • Make sure you have a .editorconfig you are happy with.
  • In the 1:Project sidepane, right-click on the folder you want to apply the .editorconfig to (e.g., the root folder of your project)
  • Choose Reformat Code, which has the default shortcut Ctrl + Alt + L.
  • Select "Include subdirectories". None of the other options are required. Then click "Run".

Your files now follow your .editorconfig file configuration.

Right-click dropdown when clicking on a folder, with Reformat Code selected

Reformat Code dialog with only "Include subdirectories" checked