132

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?

Josh Brown
  • 52,385
  • 10
  • 54
  • 80
Slee
  • 27,498
  • 52
  • 145
  • 243
  • [**here**](http://stackoverflow.com/questions/829954/how-can-i-indent-multiple-lines-in-xcode) is the best solution – swiftBoy Aug 16 '13 at 06:10
  • Possible duplicate of [Xcode source automatic formatting](http://stackoverflow.com/questions/1573968/xcode-source-automatic-formatting) – Emil Laine Dec 05 '15 at 04:14

10 Answers10

257

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
Ali Beadle
  • 4,486
  • 3
  • 30
  • 55
Vladimir
  • 170,431
  • 36
  • 387
  • 313
83
  1. select text
  2. press Control-I (the default keyboard shortcut)
Josh Brown
  • 52,385
  • 10
  • 54
  • 80
23

You can do this automatically in Xcode 11

tetrajen
  • 508
  • 6
  • 10
17

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)

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
6

Xcode 9.2:

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

Cons Bulaquena
  • 2,083
  • 2
  • 26
  • 24
6

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.

ICL1901
  • 7,632
  • 14
  • 90
  • 138
  • 3
    Exactly what I was looking for... and yet not... I was hoping I could tell Xcode to do things like indent private: and public: inside of a C++ class... instead it aligns them underneath the class keyword, but I see no such option. – ArtOfWarfare Jan 24 '13 at 17:36
5

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"

Chris
  • 806
  • 1
  • 10
  • 17
5

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.

Paul B
  • 3,989
  • 33
  • 46
  • This answer has screenshots of where to find these settings: https://stackoverflow.com/a/47023275/59913 – Stan James Dec 28 '22 at 04:02
  • This allowed me to track down why Xcode was indenting using _3_ spaces when it is configured globally for 4. – freespace Dec 29 '22 at 04:15
3

To quickly re-indent the whole file:

+a

+x

+v

Edit: No longer working in Xcode 11

Blago
  • 4,697
  • 2
  • 34
  • 29
2

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

It support format on selection.

Simple install by brew cask install swimat

Jintin
  • 1,426
  • 13
  • 22