Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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
1  
And the language is...? –  user unknown Jan 24 '12 at 16:31
2  
Similar Q&A at stackoverflow: stackoverflow.com/questions/1731357/… –  lumbric Feb 27 '12 at 15:04
add comment

3 Answers

up vote 5 down vote accepted
+25

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'
:wq
EOF

To indent files recursively, create the following script:

indent-with-vim.sh

vim -c "normal ggVG=" -e $1 <<'EOF'
:wq
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:

~/.vim/indent

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, 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"
share|improve this answer
    
Great answer, thanks! –  fdierre Aug 8 '12 at 12:59
add comment

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

share|improve this answer
1  
That only supports C, doesn't it? indent.isidore-it.eu/beautify.html –  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
add comment

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
add comment

Your Answer

 
discard

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.