Xcode Code Completion

What follows is a quick review of how I use code completion in Xcode. Chances are that options and features exist beyond what I’ll cover here, so comments and suggestions are welcome.

Let’s say I want to insert a CGRectMake method. I can begin by typing CG and pressing F5 or Option-esc, which will popup a list of possible matches:

If I choose an entry from this list, Xcode takes care of the rest. If I continue to type characters (instead of hitting F5), Xcode will further narrow the available matches (assuming there is more than one). Again, at any point I can press F5 to bring up the matching list.

As an alternative to using F5 for a list of suggestions, I can hit tab at any point and Xcode will insert the matched text currently displayed. For example, in the figure below I entered CGRe and Xcode inserted the suggestion shown below:

If I’d like to go with this suggestion, pressing the Tab key will insert the code.

Filling in Placeholder Fields
The next step is to fill in any required fields that Xcode has recognized. If there are parameters, Xcode will highlight the first field as shown:

Enter the value you want in the field and press Control-/ to move to the next field.

Code Sense Preference Settings

If you have any trouble getting these suggestions to work, I’ve included a screenshot of the Code Sense settings that I have configured in Xcode:

In the next post I’ll show how you can use various built-in macros to quickly insert blocks of code.

I’d like to thank Matt, a reader of this blog, who suggested this topic.

  1. I use Control+. to auto-complete (easy to remember: it’s right next to Control+/)

    This auto-completes without showing the pop-up list, and it automatically inserts the matched text. Continue to hit Control+. to step through the available matches (Control+Shift+. to step backwards). Undo (Command+Z) to get rid of the auto-completed text.

    It’s usually smart about which match to display first, picking the one you use most often or most recently.

    It can also auto-complete things you might not expect, like file names in #import statements, and the names of localized string (using NSLocalizedString).

  2. Hello,

    is there a way to change the shortcuts into something better?



  3. Stefan,

    You can change the shortcuts inside the Preferences settings of Xcode. Go to Preferences->Key Bindings.


  4. Hi,

    i’m really not comfortable with the default shortcuts so I changed code completion popup trigger to TAB, going to next placeholder to SHIFT+TAB and placing a regular tab in a text to ALT+TAB. It’s much easier to use cc this way (and i don’t need placing tabs that often). Perhaps that can inspire you. Also note that accepting the first suggestion is possible by hitting ENTER (RETURN) and if there are any placeholders then hitting ENTER again will move you to the first placeholder.



Comments are closed.

  • Related Content by Tag