Format code from terminal when Android Studio is running

I’ve found about this possibility here. However, it required some tweaks to get it running. Continue reading to know more.

Official code formatter description.


Having a consistent code style is important. Android Studio provides a convenient shortcut for re-formatting your code (Ctrl-Alt-L). It takes care of the code formatting including rearranging of your imports. The shortcut can be used when the file to be formatted is open in the Editor’s window of the IDE. Also, a bulk formatting can be done via the side “Android” pane. However, all that pointing and clicking is a no-go for a die-hard terminal users. Fortunately, IDEA allows for code formatting from the shell using script found in the home directory of the IDE. The problem is it can’t run, while your Android Studio is running.

The problem can’t run at the same time with Android Studio.

Why do I need to format my code from the terminal?

If you are really concerned about this question, stop reading and go live your “normies” life.

Still here? Well, I hope you are having a good laugh, cause you already know the answer – you need FULL CONTROL over code formatting operation – specifically, to which files the command is applied to. It may happen, that there is a need to apply code style to only files that are changed by the current commit. This is when having a terminal-level access to code formatting is invaluable.


Call from the root (explained later in text) of your Android Studio project:

It is a helper script that applies code formatting to the files marked as modified by git. It uses modified version of that expects a space separated list of files to format.


  1. Go to <IDE_BIN_HOME> directory (this is <where Android Studio is installed>/bin directory).
  2. cp
  3. In, uncomment following lines (they are usually the first two lines):idea.config.path=${user.home}/.AndroidStudio/config


  4. After uncommenting the lines, insert format/ before the last bit like so:idea.config.path=${user.home}/.AndroidStudio3.1/format/config


  5. Alter the above paths, so they transit through the correct .AndroidStudio folder by adding the appropriate version to it. (like .AndroidStudio3.1). Just go to your HOME and see what you got in there to know the version.
  6. Add the following to

    This effectively tricks the to believe that it is running another instance of Android Studio, thus allowing to run along the already running Android Studio (that uses I’m not sure if this is a correct explanation, but at least this is what I tell myself 🙂

    At this point can be run along with the running Android Studio. The rest are the tweaks to make life easier.

  7. Change so that it reads from standard input a list of space separated files. This is done by
    filesToFormat=$(cat -)

    Options are also saved to a variable:


    The last line is a slight modification of the original one:

    exec "$IDE_BIN_HOME/" format $options $filesToFormat
  8. Helper script is a one-liner:
    git status | grep 'modified' | awk 'ORS=" " { print $2 }' | -s .idea/codeStyles/Project.xml
    • git status | grep 'modified' – grabs all modified files.
    • awk 'ORS=" " { print $2 }' – extracts only paths (strips down modified word and all the spaces – stuff that normal git status command outputs).
    • -s .idea/codeStyles/Project.xml – modified that consumes these paths. Important bit here is the usage of the -s option that points to the code style rules to be applied. This is a lame implementation, and that’s why it is important to call from the root of your project – so that relative path to the code style will be resolved.
  9. Make sure that is in your PATH.

Here is a modified