Xcode Code Completion Macros

In the previous post I described the basics for working with code completion in Xcode. In this post I will show how you can use built-in text macros to insert various code fragments.

As an example, begin by entering ifelse into Xcode and follow this by pressing Control . (control period) and you’ll see the following code block inserted:

At this point you can add code into the placeholder fields, moving from one field to the next using Control / (control forward-slash).

If you enter a code snippet in which there is more than one possible match, you can cycle through the options by pressing F5 which will bring up a list of options in a popup window, or pressing Command . will cycle through the choices inline in your code.

For example, if you enter if and follow this by pressing F5, you will be shown the window below:

List of Available Macros

The Xcode documentation includes a list of macros available for C, C++ and Objective-C. Some of the macros I use on a regular basis are shown below:

  • nss -> NSString
  • nsms -> NSMutableString
  • nsd -> NSDictionary
  • nsmd -> NSMutableDictionary
  • a -> array (press Control-. to toggle through options)
  • log -> NSLog
  • if -> if else (press Control-. to toggle through options)

Text Macros from the Menu System
Xcode includes a menu of text macros for C, C++, HTML among others. You can find these macros in the Edit menu under Insert Text Macro.

  1. Note that you also can create your own text macros, which can be very useful.
    For example I created a TextMacro that insert touchBegan/touchMoved/touchEnded/touchCancelled definitions in my code so I can generate the template of those methods in a snap.

    Creation a a custom xctxtmacro file to define custom TextMacros is explained in the Xcode documentation (see link in the article) but there is a typo in the path they provide: you have to put your xctxtmacro file in “~/Library/Application Support/Developer/Shared/Xcode/Specifications/” and not in “~/Library/Application Support/Developer/Shared/Specifications/” otherwise it won’t work.

  2. Instead of pressing F5 to bring up a list of options, you could also use ESC (which is, in my opinion, easier to find on a keyboard when you are typing with a blindfold on ;-)

    Also, you could try F5 or ESC with other code as well, for example, if you type ‘UIV’ and press ESC you will be presented a list of classes that start with UIV.

  3. I wonder what you need to do to enable this – I am using XCode 3.2.1, and it doesn’t behave in this fashion.

    If I type ifelse, then press ctrl-., it simply beeps at me. f5 or escape tells me no completions.

    It will work for regular codesense eg. nss will bring up a list of matching symbols, but only because the first 3 letters match NSString. As such, nsms won’t work, you need to do nsmu. The putative ‘if’ macro does nothing either.

    Curiously though, log does work as a macro! It gives me NSLog(@””);

    The menu of text macros is there – they’re just largely inaccessible because they’re deep in the menu, and they don’t respond to their macro shortcut letters.

    Lastly, ~/Library/Application Support/Developer/Shared/Specifications/ doesn’t exist on my system, so I don’t even know where they are. Nothing matching xctxtmacro is present on my system, according to spotlight.

    Any hints?


  4. Ok, got it to work.

    I had to go here:


    copy C.xctxtmacro from that directory to ~/Library/Application Support/Developer/Shared/Xcode/Specifications/

    and then it all worked!

    Very nice.


  5. There is a bug on TextMacros in the latest versions of Xcode (I encourage you to send a bugreport to Apple as I did)
    In fact, only TextMacros that have the value “YES” for the key “OnlyAtBOL” will work. If this key is not present (or set to NO), the macro will not respond to autocompletion.

    Actually, the new “OnlyAtBOL” key means “Only at Beginning Of Line”, so the autocompletion for this macro will only work if it is triggered in the beggining of a line. But in fact, quite every macro I need everyday comply to this constraint (ifelse, nslog, nss, …) so this is acceptable.

    If you want those macros (that does not work because of the lack of OnlyAtBOL set to YES) to work again, you need to add/override their specifications in your “Application Support” directory (in a custom TextMacro file that override them), until the Xcode team fixes that bug.

  6. @ Julian (Feb 3, 2010 post)
    Thanks a bunch!

    Last week I upgraded and found the same problem running Xcode on my iMac. Your solution worked perfectly. The only deviation from your instructions was that I had to create the Specifications folder because it didn’t exist yet.

    Many thanks.

    – Erling

Comments are closed.

  • Related Content by Tag