clang-format is a program similar to astyle that cleans up and formats C++ code.

Currently there is support for integration with:

UPDATE 2015-2-24

As of 2015-2-24 the following editors are supported

Using the automator service method described below is only necessary if you would like to be able to use clang-format for any program using a system wide keyboard shortcut.

Installation

On OSX clang is one of the default compilers supported but the version of clang on OSX does not come with clang-format. There are two options for installing:

Homebrew:

See related homebrew pull request here

Note that as of 2014-12-03 clang-format is part the main homebrew repository, see here.

//Tap not needed as of 2014-12-03
//brew tap tcr/tcr
brew install clang-format

Manual Installation:

With this option you can copy clang-format to your /usr/local/bin directory (or a location located in your path)

Download clang binaries link

Extract and copy clang-format

Automator service (Optional):

Then open up automator and create a new service. The default should be “Service recieves selected (text) in (any application)” with “Output replaces selected text” unchecked.

Check the “Output replaces selected text” checkbox. We want to replace the original text with formatted text that astyle generates. Then drag the “Run Shell Script” into the workflow area.

In the “Run Shell Script” box replace any text with:

clang-format <&0

NOTE: you can change the automator script to change any clang-format style options or use a .clang-format file in your home directory

Save your service and open up the keyboard settings in System preferences. In the “Keyboard Shortcuts” tab find your service and set it’s shortcut.

To use the service, select any text in any text editor and use the shortcut you previously set!