Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
And the language is...? – user unknown Jan 24 '12 at 16:31
Similar Q&A at stackoverflow:… – lumbric Feb 27 '12 at 15:04
up vote 5 down vote accepted

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

To indent a single file:

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

To indent files recursively, create the following script:

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

Now, type:

$ chmod u+x
$ find . | xargs -I {} ./ {}

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 (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 satified with the result for any file type, you may look for alternatives on the web (like htb for HTML, 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"
share|improve this answer
Great answer, thanks! – fdierre Aug 8 '12 at 12:59

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

share|improve this answer
That only supports C, doesn't it? – Caesium Jan 24 '12 at 16:11
You think fdierre is searching for an universal one? – user unknown Jan 24 '12 at 16:32
Yes, I was looking for a tool that is as 'universal' as possible. I modified the question to specify that. – fdierre Jan 24 '12 at 17:01

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.