376

I wonder how can I format my code to align it neatly? Does it have a feature similar to Eclipse's ctrlshiftf?

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
James Raitsev
  • 92,517
  • 154
  • 335
  • 470

4 Answers4

742

Select first the text you want to format and then press Ctrl+I.

Use Cmd+A first if you wish to format all text in the selected file.

Note: this procedure only re-indents the lines, it does not do any advanced formatting.


In XCode 12 beta:

The new key binding to re-indent is control+I.

TheKojuEffect
  • 20,103
  • 19
  • 89
  • 125
Piotr Kochański
  • 21,862
  • 7
  • 70
  • 77
  • 2
    Thanks for this! Quick question though, do you recall where you can edit the templates that Xcode uses for its formatting? In Xcode 3 I had customized mine a bit, but I can't find the bookmark I had that said how to do it, and, the technique is probably different now. – Josh Jan 12 '12 at 20:49
  • 41
    it doesn't format code. it just reindents lines. Just like the menu item says. It doesn't take care of adding or removing extra spaces between various symbols – Radu Simionescu May 24 '16 at 08:08
  • 20
    I think it is i, Ctrl+i, not Ctrl+L – govo Oct 21 '19 at 08:03
  • 31
    Yeah, still no advanced formatting (Feb 2020)... looks like a lot of people (myself included) came to Xcode from a more engineering-oriented IDE. I personally find that Xcode is missing a lot of productivity/navigation/auto-complete features I really got used to in Eclipse/IntelliJ products... to me it feels like an Apple notepad app compared to IDEA :/ – milosmns Feb 22 '20 at 18:44
  • There is a solution! The solution is to use a Automation-Script. Check out this Tutorial for a detailed step-by-step instruction. https://luisramos.dev/xcode-format-and-save – LP Square Feb 08 '22 at 17:34
  • -1, as this is NOT a full code formatting, it is just very bad re-indentation - look into the IntelliJ Idea / Android Studio for what actually is the code formatting... – Jiří Křivánek Aug 06 '23 at 08:43
111

Key combination to format all text on open file:

Cmd ⌘ A + Ctrl I

J. Costa
  • 7,442
  • 4
  • 26
  • 31
30
  1. Select the block of code that you want indented.

  2. Right-click (or, on Mac, Ctrl-click).

  3. Structure → Re-indent

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Dair
  • 15,910
  • 9
  • 62
  • 107
20

I would suggest taking a look JetBrains AppCode IDE. It has a Reformat Code command. I have come from a C# background and used Visual Studio with Jetbrains Resharper plugin, so learning AppCode has been a pleasure because many of the features in Resharper also exist in AppCode!

Theres too many features to list here but could well be worth checking out

superlogical
  • 14,332
  • 9
  • 66
  • 76