Is any way to autoformat a source code in the Xcode IDE?
-
Possible duplicate of [Xcode source automatic formatting](http://stackoverflow.com/questions/1573968/xcode-source-automatic-formatting) – Emil Laine Dec 05 '15 at 04:16
-
Possible duplicate of [Code Formating In Xcode](http://stackoverflow.com/questions/1510915/code-formating-in-xcode) – Basil Bourque Oct 23 '16 at 00:20
8 Answers
There isn't really an autoformat option in Xcode.
There is an option ^I
(Control-I) to re-indent the code, which will re-align the code according to the tab width set in your preferences, but that's about as far as it goes.
You might have better luck with a text editor like TextMate? I don't know for certain, but I know it supports Objective-C and has some macros, there may be a reformat option?

- 13,937
- 6
- 46
- 52

- 58,279
- 31
- 157
- 188
-
2For more control of the code formatting, you can try uncrustify: http://uncrustify.sourceforge.net/ – Rob Keniger Feb 11 '10 at 09:31
-
3I keep forgetting this every time I don't use XCode for some months. And then I search again and always return to this thread. Thanks for the n-th time! :D – ecth Nov 18 '14 at 09:45
Yes Xcode 4 has an autoformat feature.
Try following:
- Select text. CMD+A.
- Press CTRL+I .
Please also see here: Fix code indentation in Xcode

- 3,996
- 8
- 45
- 86

- 1,132
- 1
- 8
- 19
-
18That's just re-indenting but not autoformatting. But good to know anyway, thanks. – huesforalice Sep 19 '12 at 10:01
One trick is to select the code (either "select all" or just the section you want to re-format) - then cut, then paste it back. This triggers the automatic code formatting in the Xcode editor.

- 208,748
- 37
- 389
- 560
-
1@Joe: oh dear - it works in Xcode 2.x and 3.x - you might want to file a bug report at http://bugreporter.apple.com – Paul R May 11 '11 at 13:48
-
@FearlessFuture: if it’s a large block of code you have to do it explicitly - there’s a menu item for doing this in recent versions of Xode so the above trick is somewhat redundant these days anyway. – Paul R Apr 15 '19 at 16:31
ClangFormat-Xcode is an Xcode plugin available in Alcatraz. This is a nice code format tool.

- 14,073
- 11
- 62
- 81
-
Also developed https://github.com/ML-Works/AnyFormat-Xcode plugin to format code with any custom script. – k06a Mar 31 '16 at 15:01
I found that the Copy & Paste method (Cmd-A then Cmd-C then Cmd-V) worked in XCode 4 and was the method I used whilst I was waiting for google to display the results (Mac was going slow!)

- 2,328
- 1
- 15
- 24
To change, go to xCode => Preferences => Text Editing => Indentation => Automatically indent based on syntax.
Format on Save
For anyone wanting to format on save this is what I did. It's a bit janky but it works well. ⤵
- To get started pick out a formatter for Xcode. I'd recommend SwiftFormat but here's a list of a bunch of them and other handy extensions.
- Once you have a formatter picked out assign it a hotkey in Xcode that's not taken such as
CMD + ~
- Then download a shortcut mapper such as BetterTouchTool.
- In your shortcut mapper simply setup
CMD + S
for Xcode and set it to actually use your shortcut you set for your fomatter. Like this ⤵

- 1,861
- 1
- 19
- 29