Can I add my own code snippets to xcode 4?

Yes, it’s quite easy – you just highlight text you want to use as a snippet and drag it into the snippets area (dragging selected text can be difficult, I find it works better if you drag from the left edge of the editor). It brings up a dialog box asking you to name it.

Also, you can have custom parameters in snippets – if you put the text <#paramName#> anywhere in the code you are dragging, when you use the snippet it will come up as a replaceable parameter that you can tab between just like in the official Snippets.

Also make sure to set a completion prefix, which makes it shorter to activate the snippet as you are typing. The nice thing about using Snippets over other solutions like global system text expanders, is that snippets can also be limited to being valid in a certain area of code – so for example a snippet that filled out a string formatting line can be marked as valid only within a block, not outside a method. That way the completion prefix only hits in areas where it’s valid to use.

You can edit a snippet by clicking once on it, a box will come up with the snippet text and some other snippet settings you can edit.

Leave a Comment