To use the command line
You need to download clang-format for MacOS and use that tool from within Terminal.app.
How to get the binaries and setup
The following stackoverflow answer discusses using Homebrew to get the MacOS binaries: Where are clang-format and clang-format.py in Mac OS X with Xcode Command Line Tools installed?
I followed the instructions on https://nacho4d-nacho4d.blogspot.de/2013/11/clang-format.html to:
- Download clang binaries for MacOS
- Move un-tarred files to ~/Development/Tools/clang+llvm-3.9.0-x86_64-apple-darwin folder
- Create an alias in .bash_login named cformat
I prefer downloading files over using Homebrew, but you can choose whatever method you want.
If you choose to download the clang binaries yourself, you may want to use a bash alias over adding the clang binaries path to the PATH variable, in case of a binary name collision.
To add the bash alias, open .bash_login in your favorite text editor and add the following line:
alias cformat=~/Development/Tools/clang+llvm-3.9.0-x86_64-apple-darwin/bin/clang-format
You would change the path to wherever you place the clang binaries.
To format all files within a project folder
From within Terminal.app, cd to the source directory you want to format and type cformat *.m
; you can use whatever wildcard you want; clang-format will output the changes it will make to the window. If you are satisfied with the results and want clang-format to change the files for you, type cformat -i *.m
and all of the Objective-C files within that folder will be formatted for you.
Settings for clang-format
The clang-format tool will look for a file named .clang-format in the immediate directory you are in and walk up to see if it can find one to know what settings it should use.
You can experiment with different settings by placing your preferences in a .clang-format file (I placed mine in my home directory) and you can look at the different clang-format styles by typing cformat -style=llvm -dump-config > ~/.clang-format
in Terminal.app.
The styles that come with clang-format include "LLVM, Google, Chromium, Mozilla, WebKit".