Given a directory containing a source tree, is there a simple way from command-line to replace each source file with its beautifully reformatted equivalent?

Keep in mind that usually a source tree for a project also contains files which are not source, and the sources themselves could be in different languages.

I'm looking for some tool that supports as many languages as possible, because a project usually contains sources in many different languages. In particular, I'd be looking for something supporting xml/html, javascript, ruby, and java.


5 Answers 5


You could use vim in ex and command mode, from the terminal.

To indent a single file:

vim -c "normal gg=G" -e <file-to-indent> <<'EOF'

To indent files recursively, create the following script:


vim -c "normal gg=G" -e $1 <<'EOF'

Now, type:

$ chmod u+x indent-with-vim.sh
$ find . | xargs -I {} ./indent-with-vim.sh {}

Vim will do its best to reindent the files. You may improve some specific file types.

For XML:

  • Install xmllint Add to your .vimrc

    au FileType xml setlocal equalprg=xmllint\ --format\ --recover\ -\ 2>/dev/null

To improve PHP formatting:

Download http://www.vim.org/scripts/download_script.php?src_id=15001 (it will download a file named php.vim)

Create the following directories on your home:


And copy php.vim to ~/.vim/indent

If you are not satisfied with the result for any file type, you may look for alternatives on the web (like htb for HTML, https://github.com/vim-ruby/vim-ruby for improvements for Ruby, and so on).

Either way, you'd use or the .vimrc technique or the foo.vim indent file to improve the indentation.

Also, you can change the find parameters to apply to some specific types only, such as:

find . -iname "*.html" -or -iname "*.xml"

Install the indent package and then run the program on the files.

  • 1
    That only supports C, doesn't it? indent.isidore-it.eu/beautify.html
    – Caesium
    Jan 24, 2012 at 16:11
  • You think fdierre is searching for an universal one? Jan 24, 2012 at 16:32
  • 2
    Yes, I was looking for a tool that is as 'universal' as possible. I modified the question to specify that.
    – fdierre
    Jan 24, 2012 at 17:01

Google Java Format

Reformats Java source code to comply with Google Java Style:


wget https://github.com/google/google-java-format/releases/download/google-java-format-1.5/google-java-format-1.5-all-deps.jar
java -jar google-java-format-1.5-all-deps.jar /path/to/MyClass.java


  • To do it on multipe files, use the --replace option, together with xargs
  • Use sponge so you can directly overwrite the input file
  • There is an Eclipse and IntelliJ plugin to perform the same reformatting

Not free, not command-line, likely too complicated, but IntelliJ Idea is an IDE that is able to do this. I haven't tested it with java or ruby, but it works quite nice with a django project (python + javascript + HTML).

It also has a quite extensive array of options to configure what to reformat.

If this is a one-time issue you can download a trial for free.

This a screen-shot of the HTML format options. It's taken from PyCharm (the python limited version, no ruby or java support here) as it's what I use, but IntelliJ should be the same but supporting much more languages.

enter image description here


to change files with 4 spaces to 2 spaces indent:

for FILE in *.cpp
    cp $FILE $ORIG
    unexpand --tabs=4 $FILE > $TMP
    expand --tabs=2 $TMP > $FILE
    diff $FILE $ORIG
/bin/rm $TMP

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .