How can I format code in Xcode like in Eclipse or NetBeans?
Asked Answered
T

11

45

How can I format code in Xcode like in Eclipse or NetBeans? I have not found any options in the menu. Is there a hot key for this, or is it simply not available in Xcode?

Treasury answered 2/10, 2009 at 17:37 Comment(3)
I should say that Xcode should a better job now! Its really not competing with Eclipse, Netbeans or Visual Studio!Treasury
Duplicate of XCode 4; How to format code?Reinert
Possible duplicate of Xcode source automatic formattingReinert
S
10

Select some text and then: Edit->Format->Re-Indent

You can bind this to a hotkey in the preferences.

Socratic answered 2/10, 2009 at 17:39 Comment(3)
This is the closest thing Xcode has to built-in formatting support.Socratic
as of 2021 if anyone want it's solution, check my answer below: https://mcmap.net/q/87703/-how-can-i-format-code-in-xcode-like-in-eclipse-or-netbeansPellmell
-1, as this is NOT the code formatting any way - look into the Android Studio for the actuall code formating capabilities...Cottonweed
W
59

In Xcode 4, it's been moved to Editor > Structure > Re-Indent command (and has a default shortcut of CTRL+I ).

Womb answered 22/7, 2011 at 22:13 Comment(0)
P
14

Well, first of all welcome to 2021. I know it's a very old question but still people like me visit this and for those people, I'm putting out multiple available solutions in one answer that worked for me after 2 hours of research and testing.

Re-Indent

As everyone else is suggesting, it's one of the default solution if you just want to re-indent the lines of code:

Goto: Editor -> Structure -> Re-Indent or use shortcut control + I

Using Xcode Extensions

There are 2 extensions I found and used which works charmingly for me.

1) SwiftFormat: mentioning the steps to install the xcode extension here from the readme.

Like the command-line tool, you can install the SwiftFormat for Xcode extension application via Homebrew. Assuming you already have Homebrew installed, type:

$ brew install --cask swiftformat-for-xcode

  • open the SwiftFormat for Xcode.app that you just installed. there you can change the rules of formatting.

  • After that, open the system preference -> extensions -> xcode source editor and mark the checkbox for SwiftFormat.

  • Restart the Xcode

Voila! You're good to go. you can use three options. format selected code, format entire file OR just lint file.

2) Swimat: (it is already mentioned in one of the answer here by @Guy Daher) steps to install from readme

Install via homebrew-cask

Homebrew latest version

brew install --cask swimat

  • Similar to above extension installation, open the Swimat.app if it does not open then try to sove it from system preferences -> security & privacy -> open swimat app

  • after opening, click on install in ./bin folder to make it work globally (not sure about this step but I just did it)

  • Restart Xcode

now you can goto editor -> swimat -> format

Bingo! You're good to go.

Additional uninvited help

You can simply set the keybinding to use any of this command. i.e. I've set the android studio default command to reformat the code as option + command + L

ps If anyone else found a better extension, please let me know in comment, I'll add it to the list.

Pellmell answered 19/2, 2021 at 0:28 Comment(2)
This sounds promising as a real code formatter (unbelievable that Xcode, after all those years, does not have such a basic feature)... Could you please recommend the better one (solely to save our time when trying to pick one)... Soory for my laziness :)Cottonweed
I used Swimat most of the time. It's my personal preference due to certain features and easily configurable.Pellmell
S
10

Select some text and then: Edit->Format->Re-Indent

You can bind this to a hotkey in the preferences.

Socratic answered 2/10, 2009 at 17:39 Comment(3)
This is the closest thing Xcode has to built-in formatting support.Socratic
as of 2021 if anyone want it's solution, check my answer below: https://mcmap.net/q/87703/-how-can-i-format-code-in-xcode-like-in-eclipse-or-netbeansPellmell
-1, as this is NOT the code formatting any way - look into the Android Studio for the actuall code formating capabilities...Cottonweed
N
9

Other than re-indentation (Edit > Format > Re-Indent), not really. However, Xcode does have support for scripts (the menu to the right of the Window menu), so you could conceivably write a script that formats your code how you like it.

Edit: here are some links that touch on this subject:

Novokuznetsk answered 2/10, 2009 at 17:41 Comment(0)
O
4

Here are the shortcuts, to format the code in Xcode

1.Format entire code (entire class/controller)

select the entire code and press +| on mac to format your code.

2.Format particular block of code

select the code and press

+] for right move and +[ for left move

Note : as per @JavierGiovannini sugesstion you can do using Editor Menu option

3.Select code --> Go to Editor --> Structure --> Re-Indent

Outward answered 16/8, 2013 at 6:3 Comment(0)
K
2

My personal favorite code formatter is Uncrustify. It has many, many options, so I also suggest you download UniversalIndentGUI, a GUI to help set Uncrustify's behavior to your liking.

Khasi answered 22/10, 2009 at 20:57 Comment(0)
S
2

XCode 8 Extensions

Another option is to use an extension like Swimat. Does the job for me. However, the caveat is that it does not format on save due to the restrictive Xcode Extension APIs, but they're trying to find a workaround.

Sleeper answered 10/5, 2017 at 8:28 Comment(0)
O
1

Not saying this is best approach, but for completeness, if you cut and then paste the code back in, Xcode will automatically format it for you.

Obovoid answered 12/1, 2016 at 15:6 Comment(0)
J
0

In v.4 you can make some adjustments through xCode preferences...

Fix code indentation in Xcode

Jellify answered 30/9, 2011 at 11:3 Comment(0)
O
0

^-I (control - capital i) : format the current line

-A + ^-I : format the whole code file

Octodecimo answered 5/5, 2020 at 23:55 Comment(0)
C
-4

Try this solution: ⌘A [⌘K ⌘F]

enter image description here

Caesura answered 13/10, 2017 at 14:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.