I would like to standardize my java code style as per Google Java Style Guide. I downloaded intellij-java-google-style.xml, made a few custom tweaks and imported it in IntelliJ in the global IDE section:
Preferences… >> Editor >> Code Style >> gear icon >> Import Scheme >> IntelliJ code style xml
This applies the Code Style to the current project. However, existing IntelliJ projects keep their existing code style. I can see the new Google Style in the other projects’ drop-down menus, but I have to manually reselect it for each project. My problem is I have hundreds of projects.
I would like to automatically select the new Google code style for all existing projects without having to go through the Preferences for each one. But how?
NOTE: I don’t want to actually auto-format the code in my projects, that would be a git nightmare.
IntelliJ does not provide a way to automatically change code style on existing local repos. So we need to think outside the box.
It turns out IntelliJ stores the active code style in
<component name="ProjectCodeStyleConfiguration"> <state> <option name="PREFERRED_PROJECT_CODE_STYLE" value="OldCodeStyle" /> </state> </component>
We can do a recursive search-and-replace on all the code style config files in a folder and replace the
value field in each. First, make sure you import the Google Style XML into IntelliJ as described in the question and call it
find recursively searches for all the paths to
sed replaces the current code style with
find ~/projects -name codeStyleConfig.xml -type f -print0 | xargs -0 sed -i "" -E "s/(.+PREFERRED_PROJECT_CODE_STYLE" value=")(.+)(" />)/1GoogleStyle3/"