How to set Xcode plugin for code auto formatting

XCode has a code formatter built in - it's a little basic but can re-indent your code. First set your indentation preferences in Xcode (as mouviciel suggests). Then select the source you want to reintend and choose Edit | Format | Re-indent.

If you want to go further, Hackertoys has instructions for adding uncrustify support to Xcode. I have not tried this.


AppCode from Jetbrains has a very good code formatter. For my taste uncrustify feels much too hacky.


updated April 2013

As Roger Nolan and rcw3 suggested, you can go a lot further with Uncrustify. The latest release version can be download here.


Integration Into Xcode

Integration into Xcode is dependent upon the version of Xcode that is installed.

  • Xcode 4.6 & newer

Benoît Bourdon's BBUncrustifyPlugin-Xcode

This plugin provides access to Uncrustify right from Xcode's "Edit" menu ("Uncrustify Selected Files", "Uncrustify Active File", and "Uncrustify Selected Lines"). Just build the project and the plugin is compiled and installed.

As of April 2, 2013, Uncrustify 0.60 and an Uncrustify configuration file are include with the plugin. To use a custom Uncrustify configuration file, make sure to read the section titled "How to customize the Uncrustify configuration?" of README.md.

Note: To use Uncrustify from the command line, for example as part of a git hook, I recommend building and installing Uncrustify independently. For consistency sake, I also recommend replacing the version of Uncrustify that comes with the plugin with a link to the version Uncrustify that was independently built and installed. The following is how to do that:

cd ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins/UncrustifyPlugin.xcplugin/Contents/Resources
mv uncrustify uncrustify.orig
ln -s /usr/local/bin/uncrustify uncrustify
  • Xcode 4.x

Jonah Williams' Code Formatting in Xcode 4

  • Xcode 3.x

Jonah Williams' Code Formatting in Xcode


Example Uncrustify Configuration File

The Uncrustify 0.60 configuration file that I use for Objective-C is located here.


Update the Uncrustify Configuration File for a New Version of Uncrustify

To update the Uncrustify configuration file for a new version of Uncrustify, start a Terminal and type:

mv uncrustify.cfg uncrustify.cfg.old
uncrustify -c uncrustify.cfg.old --update-config > uncrustify.cfg

To see the changes made to the Uncrustify configuration, in the terminal type:

diff uncrustify.cfg.old uncrustify.cfg

To View a Documented Version of the Uncrustify Configuration File

To view a documented version of the Uncrustify configuration file, start a Terminal and type:

uncrustify -c uncrustify.cfg --show-config