How to Re-Indent and Auto-Format Code in Xcode

How to Re-Indent and Auto-Format Code in Xcode

Written by

in

Quick answer: To quickly fix indentation in Xcode, press Command+A to select all text in your file, then press Control+I to re-indent. If you need full code formatting such as line wrapping and brace positioning, integrate Apple’s swift-format or SwiftFormat as an Xcode Source Editor Extension or build phase.

Why this happens

Developers coming from IDEs like Eclipse, Android Studio, or Visual Studio Code often look for a universal “format document” keyboard shortcut. In Xcode, pressing formatting keys or looking through menus frequently leaves developers confused because Xcode draws a distinct line between re-indenting and code formatting.

By default, Xcode only includes a native command to adjust leading whitespace and block indentation based on language syntax rules. It does not natively rewrite code constructs, split long parameter lists across lines, enforce space placement around operators, or rearrange modifier chains in SwiftUI declarations.

When code alignments break—often after pasting snippets, refactoring nested closures, or adjusting multi-line function signatures—running a basic re-indent command is usually all that is needed to restore structure. For comprehensive styling and linting, you must rely on external formatting tools built for the Swift compiler.

How to fix it

1. Use the native Re-Indent shortcut (Control + I)

Xcode ships with a built-in shortcut to align nested blocks and repair broken indentation levels across any supported file type (.swift, .m, .h, .json, .c).

  1. Open the source file you want to clean up.
  2. Select the lines you wish to realign. To re-indent the entire file, press Command + A (⌘A).
  3. Press Control + I (⌃I).

Xcode immediately adjusts the leading tabs or spaces for every selected line according to the nesting depth recognized by the editor.

Alternatively, you can trigger this command from the application menu:

  • Click Editor in the macOS menu bar.
  • Select Structure.
  • Click Re-Indent.
// Before Control + I
struct UserProfileView: View {
var body: some View {
VStack {
Text("Welcome")
.padding()
}
}
}

// After selecting all and pressing Control + I
struct UserProfileView: View {
    var body: some View {
        VStack {
            Text("Welcome")
                .padding()
        }
    }
}

2. Verify or customize the Re-Indent key binding

If pressing Control + I does not trigger re-indentation, another application or custom key map may have overridden the shortcut.

  1. In Xcode, open settings by pressing Command + , (⌘,) or by navigating to Xcode > Settings… in the menu bar.
  2. Select the Key Bindings tab.
  3. In the search field in the upper right, type Re-Indent.
  4. Confirm that the Structure > Re-Indent command is bound to ^I (Control + I).
  5. If the field is blank or conflicts with another command, double-click the shortcut column and press your preferred combination (such as Control + I).

3. Adjust indentation spacing preferences

If Xcode indents lines with tabs instead of spaces (or uses 2 spaces instead of your team’s standard 4 spaces), adjust the editor text settings:

  1. Open Xcode > Settings….
  2. Navigate to the Text Editing tab, then select Indentation.
  3. Under Prefer indent using, select either Spaces or Tabs.
  4. Set Indent width and Tab width to your desired values (standard Swift convention is 4 spaces).
  5. Check the box for Automatically indent based on syntax to ensure Xcode indents appropriately while you type.

4. Set up comprehensive auto-formatting with swift-format

Because Control + I only handles vertical leading whitespace, complex projects benefit from Apple’s official open-source tool, swift-format, or Nick Lockwood’s SwiftFormat. These tools reorder attributes, format line lengths, and clean up trailing commas.

To use Apple’s swift-format on your project files from the terminal or a build phase:

  1. Install swift-format via Homebrew:
    bash
    brew install swift-format
  2. Run the formatter over your project’s source directory:
    bash
    swift-format format --in-place --recursive ./Sources
  3. To check formatting rules without rewriting files (useful in CI pipelines):
    bash
    swift-format lint --recursive ./Sources

To run it automatically inside Xcode, you can add a Run Script build phase to your target or install an Xcode Source Editor Extension (such as the SwiftFormat for Xcode app available on GitHub/Mac App Store), which binds full document formatting to a custom menu shortcut.

How to prevent it

  • Keep Automatically indent based on syntax enabled in Xcode > Settings > Text Editing > Indentation.
  • Use Command + [ and Command + ] to shift selected blocks left or right without re-calculating whole-file syntax.
  • Implement a pre-commit git hook or Xcode build tool plugin running swift-format so unformatted files cannot be merged into shared repositories.
  • Agree on a .swift-format or .swiftformat configuration file committed to the root of your project directory.

FAQ

Does Control + I wrap long lines of code in Xcode?

No. The built-in Control + I shortcut is strictly a re-indent command that aligns leading horizontal spacing. It does not wrap long lines, place arguments on separate lines, or alter trailing whitespaces.

Why does Control + I fail to format a specific line or block?

If Xcode detects a severe syntax error—such as an unclosed string literal, mismatched curly brace, or broken preprocessor macro—the parser cannot determine the correct nesting scope. Fix missing braces or compiler syntax errors first, then run Control + I again.

Can Xcode format my code automatically whenever I save a file?

Xcode does not have a native “Format on Save” toggle out of the box. However, you can achieve format-on-save behavior by using an Xcode Source Editor Extension combined with macOS Automator/Shortcuts, or by adding a Swift Package Manager build tool plugin that enforces styling rules during local builds.

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.