Opening Brace Brackets on Their Own Line in Xcode 4

If you’re like me, you like your brace backets on their own lines, like this:

if (condition)
{
    statements
}

If you’re even more like me, you make heavy use of Xcode’s autocomplete and snippets, like the following:

Eb56db5a74dd42c390bedbdc90529bdb

And finally, if you’re just like me, you get really disappointed when this happens:

if (condition) {
    statements
}

What the hell, Xcode? Even the project templates have brace brackets on their own lines. What gives?

Thanks to some research, I found the file you need to edit. The problem is, Xcode is now distributed through the Mac App Store, so the steps are a little different. Here’s what you do.

  1. Open the terminal.
  2. Type cd /Applications/Xcode.app/Contents/PlugIns/IDECodeSnippetLibrary.ideplugin/Contents/Resources and hit Enter.
  3. We’re editing SystemCodeSnippets.codesnippets, but let’s make a backup first. Type sudo cp SystemCodeSnippets.codesnippets SystemCodeSnippets.codesnippets.backup and hit Enter.
  4. Use your text editor of choice to open the file as the root user.
    • Using vim, the command is sudo vim SystemCodeSnippets.codesnippets
    • Using anything else, like TextMake, it’s sudo open -a TextMate SystemCodeSnippets.codesnippets

Do a “Find” for the word “condition” and you should find almost all the instance of the snippets you want to modify. It’ll look like this:

<key>IDECodeSnippetVersion</key>
    <integer>1</integer>
   <key>IDECodeSnippetCompletionPrefix</key>
    <string>if</string>
    <key>IDECodeSnippetContents</key>
    <string>if (&lt;#condition#&gt;) {
&lt;#statements#&gt;
}</string>

Just put a new line before the opening brace bracket.

<key>IDECodeSnippetVersion</key>
    <integer>1</integer>
    <key>IDECodeSnippetCompletionPrefix</key>
    <string>if</string>
    <key>IDECodeSnippetContents</key>
    <string>if (&lt;#condition#&gt;)
{
&lt;#statements#&gt;
}</string>

This will find all the if statements and most loops; search for the text “forin” to modify the fast enumeration snippet.

Save the file and restart Xcode. That’s it!

Update : My friend Zev is maintaining an up-to-date version of SystemCodeSnippets.codesnippets for the latest Xcode version on GitHub. Go check it out.


Posted on September 10, 2012