martin carpenter

contents

most popular
2012/05/05, updated 2012/12/15
ubuntu unity lens for vim
2010/04/14
ckwtmpx

vim batch autoindent

2011/11/17, updated 2013/02/21

tags: vim autoindent

Vim's autoindent feature (the == command) can be used to batch format a collection of source files.

For example, to autoindent all of the C source and header files beneath the current working directory:

$ vim `find . -name '*.[ch]'`
:bufdo! %normal ==
:wa
:q

Update: If you want to do this on many many files (more than your shell will take as arguments to a single command) then the command above won't work. The command below uses xargs(1) to minimise the number of invocations of Vim and uses its Ex compatibility mode (-e) to execute colon commands (-c) silently (-s):

$ find . -name '*.[ch]' | xargs vim -es -c 'bufdo! %normal ==' -c wa -c q