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).
- Open the source file you want to clean up.
- Select the lines you wish to realign. To re-indent the entire file, press Command + A (
⌘A). - 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.
- In Xcode, open settings by pressing Command + , (
⌘,) or by navigating to Xcode > Settings… in the menu bar. - Select the Key Bindings tab.
- In the search field in the upper right, type
Re-Indent. - Confirm that the Structure > Re-Indent command is bound to
^I(Control + I). - 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:
- Open Xcode > Settings….
- Navigate to the Text Editing tab, then select Indentation.
- Under Prefer indent using, select either Spaces or Tabs.
- Set Indent width and Tab width to your desired values (standard Swift convention is 4 spaces).
- 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:
- Install
swift-formatvia Homebrew:
bash
brew install swift-format - Run the formatter over your project’s source directory:
bash
swift-format format --in-place --recursive ./Sources - 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-formatso unformatted files cannot be merged into shared repositories. - Agree on a
.swift-formator.swiftformatconfiguration 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.
Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.
