Sometimes projects with a long history of committers tend to collect various styles of indentation. Unfortunately not for all programming languages exist specialized tools like indent for C/C++, so we need to find a different way to mass-indent files properly. Using vim is one of them.

First you need to create a file (let's call it /tmp/indent.vim) including all the vim commands you want to run on your code. The following piece is a good start:


gg=G
:x!

Now run vim on your source code files, using the created vim script file. The following example reindents all .php files in the current folder and subfolders


find . -name '*.php' -exec vim -s /tmp/indent.vim {} \;

If you don't like the result it might be that the indentation settings in you .vimrc don't suit your needs. Of course you are able to add other fancy vim commands to modify your files - like adding or modifying copyright headers.

one-liner version

vim -s <(echo gg=G:wq)

But that depends on Bash for the process substitution.

Comment by Paul Hedderly