itsaboutcode
itsaboutcode

Reputation: 25109

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

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?

Upvotes: 46

Views: 35365

Answers (12)

Kristopher Johnson
Kristopher Johnson

Reputation: 82565

In Xcode 16, the Control-I shortcut for Editor > Structure > Re-indent is still supported, but there is also a new Editor > Structure > Format File with 'swift-format' command, with a shortcut of Shift-Control-I.

Upvotes: 0

Keval Langalia
Keval Langalia

Reputation: 1892

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.

Upvotes: 17

themefield
themefield

Reputation: 4275

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

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

Upvotes: 0

Dave DeLong
Dave DeLong

Reputation: 243166

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:

Upvotes: 9

Ruth Vargas
Ruth Vargas

Reputation: 1

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

enter image description here

Upvotes: -4

Guy Daher
Guy Daher

Reputation: 5601

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.

Upvotes: 2

Shaheen Ghiassy
Shaheen Ghiassy

Reputation: 7517

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.

Upvotes: 1

swiftBoy
swiftBoy

Reputation: 35783

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

Upvotes: 4

geowar
geowar

Reputation: 4457

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.

Upvotes: 2

ICL1901
ICL1901

Reputation: 7778

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

Fix code indentation in Xcode

Upvotes: 0

Dylan Markow
Dylan Markow

Reputation: 124479

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

Upvotes: 60

nall
nall

Reputation: 16149

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

You can bind this to a hotkey in the preferences.

Upvotes: 10

Related Questions