88

Xcode code formatter Swift

I'm trying to auto format my code written in Swift. It's necessary if you're working in team and using CVS and don't want to merge for hours.

Ctrl+I just makes an indent, but for example i want to have proper space between functions and just all the stuff like in Clang.

Aleksandr Kravets
  • 5,750
  • 7
  • 53
  • 72
S. Birklin
  • 1,255
  • 1
  • 13
  • 19
  • Does this answer your question? [Code Formatting In Xcode](https://stackoverflow.com/questions/1510915/code-formatting-in-xcode) – Keval Langalia Feb 19 '21 at 00:38

10 Answers10

89

The reformat code command in xcode is Ctrl-I, not Cmd-I and it appears to work just fine.

CuriousRabbit
  • 2,124
  • 14
  • 13
  • 1
    Yeah, that's what i meant. Ctrl-I. – S. Birklin Nov 24 '14 at 12:45
  • 22
    Control + I seems to perform indentation on Swift and Objective C, but I would like to see a more complete syntactical prettification ala Visual Studio and Resharper in Xcode - but with that being said, I think Xcode is on its way to becoming a usable IDE. – Shawn J. Molloy May 09 '15 at 23:39
  • 1
    Ctrl+I seems to have no effect on a text selection for me in XCode 7 beta 5 – codecowboy Aug 14 '15 at 12:22
  • 2
    If you are using XCode, you can try my plugin: Swimat - https://github.com/Jintin/Swimat – Jintin Jan 01 '16 at 02:06
  • 5
    control-I **does not reformat the code**, it merely "balances" each line. so, it does not enforce all the necessary newlines, indentation and so on that is part of the standard idiom for control structures in Swift. (confusingly, Xcode has the ability to automatically format your Swift as you type, but there's no button to "fix" batches of it later!) – Fattie Feb 21 '17 at 13:56
  • Xcode is still no where close to VSCode, they just fixed multi tabbing in Xcode 12. May be by Xcode 100 Pro Max Plus we will have format on save. – user5381191 Oct 15 '20 at 06:45
  • 1
    It's an upper i, not a lower L. – lsaudon Dec 09 '22 at 10:18
  • Yea this is blatantly an incorrect answer; ^I doesn't format beyond indentation at all. – gnkdl_gansklgna Jan 25 '23 at 07:21
63

I write a extension app about format Swift code: Swimat

There has two way to install

  1. brew install swimat --cask

  2. Clone this repo, build and restart Xcode.

See https://github.com/Jintin/Swimat for more information

preview

Michiel de Mare
  • 41,982
  • 29
  • 103
  • 134
Jintin
  • 1,426
  • 13
  • 22
  • 1
    I think you should mention or illustrate if Swimat handles formatting spaces between lines. That information is not illustrated or communicated either here or on the github page. As per the gif illustration, Xcode default formating handles that already. – user3144836 Feb 10 '17 at 23:41
  • Swimat will do as default Xcode do, which is remove empty line space. But not remove empty line. Thank you for your feedback, we'll update the description later. – Jintin Feb 11 '17 at 02:43
  • Nice, I wish there is one for Objective-C too. – CyberMew Apr 07 '17 at 04:14
  • 2
    dude, you did awesome job. why no one else is pointing to this solution? there are several open question on formatting in xcode. – Keval Langalia Feb 18 '21 at 23:37
28

In Xcode [Objective-C, Swift]:

1) +A: highlight code

2) +I: reformat code

Hamid Yusifli
  • 9,688
  • 2
  • 24
  • 48
  • 3
    this should be the answer even for in 2021. You have to select the code (cmd + A) first then ctrl + I will format it for you. – dragonfly02 Nov 27 '21 at 06:14
  • 9
    ⌃+I: only reindents, doesn't reformat. At least for swift anyway. Try for example to reformat: ```struct Tag {var name: String }``` It won't do anything – Sergio Martins Dec 16 '21 at 18:45
21

+i: reformat code
Ctrl+i: fix code indentation

Eventhough it has good IntelliSense, xcode sucks at code formatting generally.
Wish it was better.
Apple has to spend so much effort in this in order to make it VS level good.

Add080bbA
  • 1,818
  • 1
  • 18
  • 25
  • Control + i does work with Swift. In what way do you see code formatting working better with Objective C as then Swift? Just curious. – Shawn J. Molloy May 09 '15 at 23:48
  • 2
    `Control + i` only indents code in swift. It does not fix spaces after brackets etc. – Simon Bengtsson Feb 22 '17 at 13:10
  • 7
    ^ just means ctrl--why are these listed as separate shortcuts? Both are the exact same key combinations. ^i re-indents--it does *not* reformat. – Zenexer Feb 16 '21 at 01:31
13

Xcode and Swift 2.0: Per file, Cmd-A then Ctl-I. Ctl-I works for what is highlighted, Cmd-A highlights all. Just be careful not to accidentally delete all your source after the Ctl-A!

Antonio Ciolino
  • 546
  • 5
  • 16
11

SwiftFormat is a popular code formatter for swift. It has an extension for Xcode, which can be accessed from the Editor menu.

Quote from the SwiftFormat readme:

SwiftFormat is a code library and command-line tool for reformatting swift code on macOS or Linux.

SwiftFormat goes above and beyond what you might expect from a code formatter. In addition to adjusting white space it can insert or remove implicit self, remove redundant parentheses, and correct many other deviations from the standard Swift idioms.

askielboe
  • 873
  • 13
  • 14
  • 1
    Any clue why this isn't built into xcode? this would be far more useful than the mostly-useless indent functionality they have built-in. – gnkdl_gansklgna Jan 25 '23 at 07:23
0

(I know the question says Xcode but) personally this is another reason to use AppCode alongside Xcode.

While Xcode only fixes the indentation for Swift code, as mentioned in the question, AppCode at least handles spaces too. Somewhat relatedly it removes indents from empty lines by default.

AppCode's Swift support definitely isn't perfect yet and you still need to check the code in both, but it's still worth it because of better navigation and things like this.

Toerndev
  • 715
  • 1
  • 6
  • 21
-2

The command in xcode is ctrl-i to format code, not cmd-i.

ashchk
  • 161
  • 13
-4

I would suggest using sublime text with the swift plugin, which will allow you to hotkey the code formatting. https://sublime.wbond.net/packages/Swift

user2961953
  • 155
  • 1
  • 2
  • 6
  • 3
    I don't think this is very efficient - you're saying OP would have to open each file (individually) in a separate program just to format them? – Craig Otis Nov 22 '14 at 23:17
  • The suggestion was not to re-open them. Developers at my job work in sublime text, not xcode. – user2961953 Nov 24 '14 at 20:40
  • 5
    To be fair though, OP was asking about how to auto-format his code in *Xcode*. So to suggest that he do so in Sublime Text is either saying (1) he open them separately, or (2) he switch editors. – Craig Otis Nov 24 '14 at 20:46
  • 3
    I am not so much concerned about "being fair" (fair to who?) as offering a solution. Sometimes it is necessary to think outside the box. This solution might fall into that category. Thanks for your opinion though. – user2961953 Nov 26 '14 at 16:44
  • I just install swift plugin for Sublime, but seem like Sublime doesn't have hotkey or menu to make the format. – Anh Bảy Oct 08 '15 at 02:57
-6

Select lines, then press Ctrl+I.

mario
  • 9,858
  • 1
  • 26
  • 42
  • 9
    When answering a four year old question with eight other answers please take the time to make sure your answer isn't a duplicate, and if it isn't to point out what new aspect of the question your answer addresses. Also formatting for keystroke combinations can be done with the use of `` and `` around each key. – Jason Aller Oct 19 '19 at 16:05