Mild-Mannered Canadian Fury

Doug Stephen is Politely Peeved

Xcode 4.3: Place All Autocompleted Opening Curly Braces on New Lines


Wed, 16 May 2012 «permalink»

Apple’s Objective-C style convention for code blocks is to place the opening curly brace for new blocks on the same line as the beginning block statements life if clauses or method definitions, and this is enforced by their Xcode IDE’s autocomplete/”Intellisense” behavior.

I don’t care for this. I don’t have any claims to empirical/objective evidence that this is bad style. I just have a subjective preference to have all of my opening curly braces start on new lines, whether it be for decision statements, class declarations, method implementations… it doesn’t really matter. It’s the way I like it. This could have something to do with the fact that this is our official style guide practice for blocks at the lab, and so it’s just what I’ve become accustomed to looking at.

I’ve been digging around for a way to fix this for a while, and while I got a lot of hits on how to do this using defaults write directives from the shell, none of these worked. Come to find out, Xcode 4 doesn’t use those defaults anymore, and instead stores its autocomplete behavior in Code Snippets. It turns out that, even though Xcode won’t let you edit the default code-snippets normally, they are really just plists and so if you’re brave you can dig in and tweak them yourself in your editor of choice.

So, that’s what I did. With a little find-replace magic. And I’m going to provide you with a copy of my new Snippet library plist, but not without explaining the process first.

First, you should absolutely back up your default snippet library, just in case you decide you want to go back to the old way or you completely bork something. Second, you should make a backup of your new version, since the old version will get wiped out any time Xcode gets updated.

The above Stack Overflow Q&A thread makes mention of the fact that you’ll need to make “half a dozen” or more edits to cover all of the most common cases. But common cases weren’t enough for me. I wanted uniformity. This isn’t hard to achieve if you have a half-decent text editor. I did this using Sublime Text 2. Make your way to the plist as per the instructions in the Stack Overflow answer, fire it up in your editor, and then use a little regex-mode Find/Replace All to get what you want: Look for any opening curly braces with a preceding space character, something like [ ]{. Then replace all of these occurences with a newline and a curly brace. As of Xcode 4.3, this was 34 occurences; this is because not only do you need to manage the explicit cases, but the implicit ones as well, such as snippets that have nested decisions or loops.

And that’s it. Really. Save the plist, restart Xcode, and blam.

Lastly, as promised, if you’d prefer to not muck about with all this and just have the work done for you, you can download my edited version of the stock Snippets Plist here.