How to call clang-format over a cpp project folder?

user3639557 picture user3639557 · Mar 6, 2015 · Viewed 61.9k times · Source

Is there a way to call something like clang-format --style=Webkit for an entire cpp project folder, rather than running it separately for each file?

I am using clang-format.py and vim to do this, but I assume there is a way to apply this once.

Answer

Antimony picture Antimony · Mar 16, 2016

Unfortunately, there is no way to apply clang-format recursively. *.cpp will only match files in the current directory, not subdirectories. Even **/* doesn't work.

Luckily, there is a solution: grab all the file names with the find command and pipe them in. For example, if you want to format all .h and .cpp files in the directory foo/bar/ recursively, you can do

find foo/bar/ -iname *.h -o -iname *.cpp | xargs clang-format -i

See here for additional discussion.