Fix code indentation in Xcode

Xcode

Xcode Problem Overview


Once I start editing my code and adding for loops or if then statements my code indentation is whacked because the previous code maintains its former indentation instead of adjusting automatically.

In Visual Studio you can highlight code and select 'format selection' to fix these issues - is there anything similar in Xcode?

Xcode Solutions


Solution 1 - Xcode

Before Xcode 9 (for Xcode 9 see default shortcut below)

  1. select text
  2. right-click
  3. there's a "Re-indent selection" item in popup menu

In XCode 4 there is one more step:

  1. select text
  2. right-click
  3. Click on "Structure"
  4. there's a "Re-indent selection" and other options in popup menu

Or using the default shortcut:

  1. select text
  2. press ctrl + i

Solution 2 - Xcode

  1. select text
  2. press Control-I (the default keyboard shortcut)

Solution 3 - Xcode

You can do this automatically in Xcode 11

Solution 4 - Xcode

In Xcode 4.6.3, choose Editor > Structure > Re-Indent.

Shortcut: Control + I (that's 'i', for 'indent')

This works on current selection. If no text selected, choose Edit > Select All (⌘+A) and then perform the re-indent.

(I'm more clearly restating the other answers)

Solution 5 - Xcode

These solutions re-indent text to the default xCode behaviors. To change these behaviors, go to xCode (v.4) > Preferences > Text Editing. Then to Indentation Tab.

Solution 6 - Xcode

Xcode 9.2:

You can select text and press ctrl + i to re-indent code.

Solution 7 - Xcode

To quickly re-indent the whole file:

+a

+x

+v

Edit: No longer working in Xcode 11

Solution 8 - Xcode

You can do it in different ways, but these two are the easiest

  • select text ( +a) :
  1. Using shortcut -> (control)^ + i

  2. Click on "Editor" -> Click on "Structure" -> Click on "Re-indent"

Solution 9 - Xcode

You can try my extension Swimat https://github.com/Jintin/Swimat

It support format on selection.

Simple install by brew cask install swimat

Solution 10 - Xcode

Sometimes setting Xcode global preferences is not enough for ^ + i to do its job accordingly. The reason is that project settings override global ones. You can set indentations for a concrete project or even a file. Select project (or file) in the Navigation Pane. Then in File Inspector go to Text Settings section and set desired number of tabs or spaces for indents.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSleeView Question on Stackoverflow
Solution 1 - XcodeVladimirView Answer on Stackoverflow
Solution 2 - XcodeJosh BrownView Answer on Stackoverflow
Solution 3 - XcodetetrajenView Answer on Stackoverflow
Solution 4 - XcodeBasil BourqueView Answer on Stackoverflow
Solution 5 - XcodeICL1901View Answer on Stackoverflow
Solution 6 - XcodeCons BulaquenaView Answer on Stackoverflow
Solution 7 - XcodeBlagoView Answer on Stackoverflow
Solution 8 - XcodeChrisView Answer on Stackoverflow
Solution 9 - XcodeJintinView Answer on Stackoverflow
Solution 10 - XcodePaul BView Answer on Stackoverflow