Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Legacy Documentclose button

Important: The information in this document is obsolete and should not be used for new development.

Indenting Code

Xcode’s editor supports syntax-aware indenting to make it simple to author neat and readable code. When you use syntax-aware indenting, Xcode automatically indents and formats your code as you type; pressing Return or Tab moves the insertion point to the appropriate level by examining the syntax of the surrounding lines. You can also choose to indent code manually.

This section shows you how to configure syntax-aware indenting, how to manually format text in the editor, and how to control the format of tabs and automatic indentation.

In this section:

Syntax-Aware Indenting
Indenting Code Manually
Setting Tab and Indent Formats


Syntax-Aware Indenting

Xcode gives you a number of ways to control how it automatically formats your code. You can control which characters cause Xcode to indent a line, what happens when you press the Tab key, and how Xcode indents braces and comments.

Syntax-aware indenting is not enabled by default; to turn it on, choose Xcode > Preferences, click Indentation, and select the “Syntax-aware indenting” option. For more information on the options available in the Indentation preferences pane, see “Indentation Preferences.”

Choosing What the Tab Key Does

When you use syntax-aware indenting, you usually press the Tab key to tell the editor to indent the text on the current line. But when you’re at the end of the line, you may want to insert a tab character before, say, you insert a comment. To choose the circumstances when pressing the Tab key reindents a line, open the Indentation pane of Xcode Preferences and use the “Tab indents” menu in the syntax-aware indenting options. You can choose the following options:

To insert a tab character regardless of this option’s setting, press Option-Tab. Similarly, to perform syntax-aware indenting, regardless of this option’s setting, press Control-I.

Choosing How to Indent Braces

You can have Xcode automatically indent braces to help you easily see the level of nesting in your code and keep your code readable. In addition, to help you keep braces balanced, you can opt to have Xcode automatically insert a closing brace when you type an opening brace.

To choose how much an opening brace is indented when it appears on a line by itself, choose Xcode > Preferences, click Indentation, and use the “Indent solo ‘{’ by:” field. If this field is greater than 0, Xcode automatically indents opening braces to the level of the previous line plus the specified number of characters. By default, the value of this field is 0.

To choose whether to insert a closing brace automatically when you type an opening brace, choose Xcode > Preferences, click Indentation, and use the “Automatically insert closing ‘}’” option.

Choosing Which Characters Reindent a Line

To choose which characters cause Xcode to a automatically indent a line whenever they’re typed, choose Xcode > Preferences, click Indentation, and use the “Automatically indented characters” options.

Choosing How to Indent C++-Style Comments

You can choose how to indent C++-style (//) comments when they appear on lines by themselves. You cannot automatically indent C++-style comments that appear at the end of code lines.

To automatically indent C++-style comments that appear on lines by themselves, choose Xcode > Preferences, click Indentation, and use the “Indent // comments” option.

To align consecutive C++-style comments that appear on lines by themselves, choose Xcode > Preferences, click Indentation, and use the “Align consecutive // comments” option.

Both these options are on by default when syntax-aware indenting is enabled.

Indenting Code Manually

If you choose not to use syntax-aware indenting, you must do any indentation and formatting manually. When syntax-aware indenting is disabled, pressing Tab inserts a tab and pressing Return inserts a carriage return and moves the cursor to the same level as the previous line. You can also indent a block of text to the left or right by selecting the text and choosing Format > Shift Left or Format > Shift Right.

When syntax-aware indenting is turned off, Xcode may still indent newly added lines to the level of the previous line when you press Return. To turn this off, add the Return key to the key-equivalents list of the Insert Newline action in Key Bindings preferences. For information on configuring key bindings for actions, see “Customizing Key Equivalents.”

Setting Tab and Indent Formats

Whether you indent a line manually, or rely on Xcode’s syntax-aware indenting, you can control the width of tabs and indents, as well as whether Xcode inserts Tab characters or spaces. You can specify default values for all files you open in Xcode, as well as customizing these settings for individual files.

Changing the Indent and Tab Width

You can set how many spaces to indent when the editor automatically indents or when you press the Tab key. To set the default indent or tab width for every file you open, open the Indentation pane of Xcode Preferences and change the “Tab width” or “Indent width” setting.

To override the default indent or tab width for one or more specific files, select the files in the Groups & Files list and open the inspector window. In the General pane, change the Indent Width or Tab Width setting.

If you change a file’s default indent or tab width, those settings are in effect for everyone who views that file.

Using Spaces Instead of Tabs

The editor can insert a series of spaces instead of a tab whenever it indents code or you press Tab. This ensures that your code looks the same on other computers no matter how wide their tabs are set. However, this means that changing the width of tabs won’t affect code you’ve already written.

To specify that the editor uses spaces instead of tabs, choose Xcode > Preferences, click Text Editing, and select “Insert ‘tabs’ instead of spaces” option. These options are saved in your own preferences but not in the file itself. When other people edit the file, their preferences for that file take effect.

You can also specify this setting on a per-file basis. To choose whether the editor uses tabs or spaces when editing a certain file, select the file in the Groups & Files list, open the inspector window, and select “Editor uses tabs.”



< Previous PageNext Page > Hide TOC


Last updated: 2006-11-07




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice