How to format code in Xcode? [duplicate]
Asked Answered
M

4

401

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

Massachusetts answered 1/7, 2011 at 2:28 Comment(1)
I have gathered all available options as in Feb 2021 in this post: https://mcmap.net/q/87703/-how-can-i-format-code-in-xcode-like-in-eclipse-or-netbeansBlodgett
R
787

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.

Radman answered 19/10, 2011 at 21:23 Comment(6)
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.Doha
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 symbolsRider
I think it is i, Ctrl+i, not Ctrl+LCirri
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 :/Sibie
There is a solution! The solution is to use a Automation-Script. Check out this Tutorial for a detailed step-by-step instruction. luisramos.dev/xcode-format-and-saveTornado
-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...Schnorrer
N
119

Key combination to format all text on open file:

Cmd ⌘ A + Ctrl I

Nowak answered 7/6, 2013 at 14:33 Comment(0)
P
31
  1. Select the block of code that you want indented.

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

  3. Structure → Re-indent

Profanity answered 1/7, 2011 at 2:32 Comment(0)
H
22

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

Haggerty answered 3/1, 2013 at 22:12 Comment(2)
Good tip but doesn't really answer the question.Lanfri
Unfortunately, AppCode sunsetted.Pecksniffian

© 2022 - 2024 — McMap. All rights reserved.