Re-indenting files with vim

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:


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.

Bernd Zeimetz

Debian Developer | Senior Linux Systems Engineer & Consultant

Salzburg, Austria