How to reformat/reindent all the source files in a given directory?
Solution 1:
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'
:wq
EOF
To indent files recursively, create the following script:
indent-with-vim.sh
vim -c "normal gg=G" -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 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"
Solution 2:
Install the indent
package and then run the program on the files.