Getting Xcode to put opening braces on a new line

By default Xcode (the Apple Developer IDE) puts opening braces at the end of the current line like so:


if ( condition ) {
     // code here...
}

However I have been working on projects for years with a different convention:


if ( condition )
{
    // code here...
}

Of course developers have different preferences here. One is not right and the other wrong – that is dictated by a company or by a project, or by the consensus of the developers starting a new project. Once a convention is in place then it must be adhered to for the life of the project. And of course a decent IDE will work with developers preferences.

So – how do we get Xcode to support this alternate convention? The problem being that there is no UI preference to set this.

To change this preference the quick way:

Open the Terminal App and paste the following code:


defaults write com.apple.Xcode XCCodeSenseFormattingOptions -dict BlockSeparator "\n"

Then restart Xcode. Thats it.

What does this do? It places a new entry in a plist file that is located in your user Home Directory. Home > Library > Preferences > com.apple.Xcode.plist.

Next time you start coding in Xcode it will autocomplete with opening braces on a new line.

5 Responses to “Getting Xcode to put opening braces on a new line”

  1. credo Says:

    Worked like a charm. Thanks.

  2. Brother Mugga Says:

    Excellent. Much better.

    How do we change it back, however? I’m a total beginner, so although I think I can see what to type I don’t want to muck it up.

    Ta.

  3. mike42 Says:

    Great, many thanx!

    @Brother Mugga: To change it back you can execute

    defaults delete com.apple.Xcode XCCodeSenseFormattingOptions

    in the Terminal.

  4. yindigo Says:

    This worked great..Thanks

  5. Joe Says:

    To reverse the action, just put the space back into the plist:

    write com.apple.Xcode XCCodeSenseFormattingOptions -dict BlockSeparator ” “

Leave a Reply