Why this happens
If you move to Xcode from IDEs like Visual Studio Code, IntelliJ, Android Studio, or Eclipse, highlighting a block of code and pressing the Tab key produces an unpleasant surprise: Xcode deletes the entire selection and replaces it with a single tab or set of spaces.
Xcode follows standard macOS text-editing behavior inherited from Cocoa text views. On macOS, Tab is treated as a character input rather than a structural formatting command when text is highlighted. Just like pressing the Space bar or a letter key while text is selected, pressing Tab overwrites the active selection.
If you just wiped out a block of code by accident, press Command + Z (⌘Z) immediately to restore it. Instead of Tab and Shift + Tab, Xcode provides dedicated shortcuts for manual shifting, syntax-based re-indentation, and multi-cursor editing.
How to fix it
1. Shift lines manually with Command + ] and Command + [
Xcode maps manual block indentation to the square bracket keys. These commands move every line in your selection by one indent level (tabs or spaces, depending on your project settings) without altering the code itself.
- Highlight the lines you want to move—you do not even need to select the full first or last line; any partial selection across those lines works.
- Press
Command + ](⌘]) to indent (shift right). - Press
Command + [(⌘[) to unindent (shift left).
You can also trigger both actions from the menu bar under Editor > Structure > Shift Right and Editor > Structure > Shift Left.
For example, if you wrap existing Swift code inside a new if statement or Task block, highlight the inner lines and press ⌘]:
import Foundation
func syncRecords(isAuthorized: Bool) {
if isAuthorized {
// Select these two lines and press Command + ]
let timestamp = Date()
print("Sync started at \(timestamp)")
}
}
After pressing Command + ], both lines move cleanly to the next indentation stop:
import Foundation
func syncRecords(isAuthorized: Bool) {
if isAuthorized {
let timestamp = Date()
print("Sync started at \(timestamp)")
}
}
2. Auto-format code structure with Control + I (Recommended)
In most Swift and SwiftUI files, manually shifting lines left or right is slower than letting Xcode calculate the exact indentation from your syntax tree. When braces, closures, or chained SwiftUI view modifiers get out of alignment, manual shifting preserves uneven spacing between lines, whereas Xcode’s Re-Indent command fixes every line relative to its enclosing scope.
- Select the lines you want to clean up, or press
Command + A(⌘A) to select the entire file. - Press
Control + I(⌃I), or go toEditor > Structure > Re-Indentin the menu bar.
Consider a SwiftUI view where pasted code has inconsistent spacing across nested stacks and modifiers:
import SwiftUI
struct ProfileHeaderView: View {
let username: String
var body: some View {
VStack(alignment: .leading, spacing: 8) {
Text(username)
.font(.headline)
Text("Active Developer")
.font(.subheadline)
.foregroundStyle(.secondary)
}
.padding()
}
}
Using Command + ] here would push every line to the right while keeping the broken relative offsets. Highlighting the file with Command + A and pressing Control + I aligns every view and modifier to its proper scope in one step:
import SwiftUI
struct ProfileHeaderView: View {
let username: String
var body: some View {
VStack(alignment: .leading, spacing: 8) {
Text(username)
.font(.headline)
Text("Active Developer")
.font(.subheadline)
.foregroundStyle(.secondary)
}
.padding()
}
}
Tip: If Control + I stops indenting properly mid-file, you almost always have a missing or extra curly brace ({ or }) or parenthesis right above the line where the indentation breaks.
3. Fix shortcuts on non-US keyboards via Key Bindings
On many non-US keyboard layouts (such as German, Nordic, Spanish, or French ISO keyboards), typing [ and ] requires holding Option (⌥) plus a number key. That makes pressing Command + [ or Command + ] either awkward or impossible because the OS intercepts the combination.
You can rebind Shift Right and Shift Left to accessible keys in Xcode:
- Open
Xcode > Settings...(orPreferences...in older versions) from the menu bar, or pressCommand + ,(⌘,). - Click the Key Bindings tab at the top of the window.
- Click the All filter button and type
Shift Rightinto the search field in the upper-right corner. - Locate Shift Right (Structure) and Shift Left (Structure) under the Editor menu group.
- Double-click the shortcut column on the right side of each row and press your preferred key combination.
Avoid binding plain Tab to Shift Right. If you assign Tab directly to Shift Right, pressing Tab on a single empty line will shift the line rather than inserting tabs or navigating autocomplete placeholders. If you want a familiar shortcut that will not clash with code completion, map an unused combination with Control or Command (for example, Control + Tab or Command + ', checking Xcode’s conflict warning banner at the bottom of the Key Bindings pane).
4. Indent with multi-cursor editing
When you want to insert whitespace or align specific columns inside lines (such as aligning dictionary values or inline comments) rather than shifting whole lines from the left margin, use Xcode’s multi-cursor support.
- Column selection: Hold
Option(⌥), then click and drag vertically across the lines where you want cursors to appear. - Keyboard multi-cursor: Place your cursor on the first line, then press
Control + Shift + Down Arrow(⌃⇧↓) orControl + Shift + Up Arrow(⌃⇧↑) to add cursors on adjacent lines. - Manual placement: Hold
Control + Shiftand click exact spots on different lines.
As long as multiple insertion points are blinking without characters highlighted across them, pressing Tab or Space inserts indentation at every cursor simultaneously without deleting code. Press Escape when finished to return to a single cursor.
How to prevent it
You can reduce how often you need to manually indent blocks by tuning Xcode’s editor settings under Xcode > Settings... > Text Editing > Indentation:
- Enable “Automatically indent based on syntax”: Keeps new lines aligned as you type closures,
switchcases, and SwiftUI builders. - Check “Indent” on paste: Ensures code copied from browsers, documentation, or other files is automatically run through Xcode’s re-indenter the moment you press
Command + V. - Standardize “Prefer indent using”: Choose Spaces (Xcode’s default is 4 spaces) or Tabs across your team, and match project-level overrides in the File Inspector (
Option + Command + 1under Text Settings) so pasted or shifted lines never mix tabs and spaces. - Format Swift files automatically on build or save: For larger teams, integrate Apple’s
swift-formattool to enforce uniform indentation across the entire repository without manual fixes.
FAQ
Can I make Xcode’s Tab key indent selected lines like VS Code?
No, Xcode’s key-binding system does not support conditional bindings that distinguish between an active multi-line selection and a single insertion cursor. Assigning Tab to Shift Right in Key Bindings overrides normal tab insertion and interferes with autocomplete parameter jumping, so using Command + ] or Control + I is the safest workflow.
Why did Control + I push all my Swift code to the wrong indent level?
Control + I parses the surrounding Swift syntax to calculate depth, so an unclosed string literal, missing parenthesis, or mismatched curly brace earlier in the file throws off every line below it. Scroll up to the exact line where indentation first looks wrong to find the syntax error.
How do I convert existing tabs to spaces when re-indenting a file?
Select the code (Command + A), then verify your preferred setting in the File Inspector (Option + Command + 1 > Indent Using). Once set to Spaces or Tabs, run Editor > Structure > Re-Indent (Control + I) to rewrite the leading whitespace of every selected line using that setting.
Parts of this guide are adapted from content licensed under CC BY-SA 3.0; this article is shared under the same license.
