109

Is any way to autoformat a source code in the Xcode IDE?

sashaeve
  • 9,387
  • 10
  • 48
  • 61
  • 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 Answers8

124

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?

Annika Backstrom
  • 13,937
  • 6
  • 46
  • 52
Jasarien
  • 58,279
  • 31
  • 157
  • 188
  • 2
    For more control of the code formatting, you can try uncrustify: http://uncrustify.sourceforge.net/ – Rob Keniger Feb 11 '10 at 09:31
  • 3
    I 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
70

Yes Xcode 4 has an autoformat feature.

Try following:

  1. Select text. CMD+A.
  2. Press CTRL+I .

Please also see here: Fix code indentation in Xcode

Ayrad
  • 3,996
  • 8
  • 45
  • 86
Artem Kalachev
  • 1,132
  • 1
  • 8
  • 19
31

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.

Paul R
  • 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
9

ctrl + I will auto format the code you're selecting

Yiyue Wang
  • 99
  • 1
  • 4
4

ClangFormat-Xcode is an Xcode plugin available in Alcatraz. This is a nice code format tool.

chancyWu
  • 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
2

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!)

netniV
  • 2,328
  • 1
  • 15
  • 24
0

To change, go to xCode => Preferences => Text Editing => Indentation => Automatically indent based on syntax.

enter link description here

Community
  • 1
  • 1
Indra
  • 78
  • 4
0

Format on Save

For anyone wanting to format on save this is what I did. It's a bit janky but it works well. ⤵


  1. 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.
  2. Once you have a formatter picked out assign it a hotkey in Xcode that's not taken such as CMD + ~
  3. Then download a shortcut mapper such as BetterTouchTool.
  4. 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 ⤵ enter image description here
tyirvine
  • 1,861
  • 1
  • 19
  • 29