Xcode Code Editing Shortcuts
Beyond navigation, Xcode's text editor carries a standard but genuinely useful set of code-manipulation shortcuts for commenting, reformatting, reordering, and temporarily collapsing sections of Swift or Objective-C source. Two more shortcuts round out everyday editing: quickly duplicating a line to build a similar statement without retyping it, and jumping directly to a known line number referenced by a compiler error or crash log.
| Action | Windows | Mac | Description |
|---|---|---|---|
| Comment/uncomment selected lines | — | Cmd+/ | Drops a // in front of whichever line the cursor sits on, or every line in the current selection if several are highlighted, so you can disable code temporarily without deleting anything. |
| Re-indent selected code | — | Ctrl+I | Reformats the selected code — or the entire file when nothing's highlighted — to match Xcode's configured indentation rules, handy for cleaning up code pasted in from a source with different formatting conventions. |
| Move line up | — | Cmd+Option+[ | Moves the current line or selected block up by one line, reordering code without cutting and pasting manually. |
| Fold/unfold current code block | — | Cmd+Option+Left / Right | Collapses the current function, class, or code block down to a single summarized line, or expands a previously folded block back open, useful for temporarily hiding irrelevant code while focusing on a specific section. |
| Duplicate current line/selection | — | Cmd+Ctrl+Shift+/ (varies) or Edit menu > Duplicate | Creates a copy of the current line or selection directly below it, faster than manually selecting, copying, and pasting for quickly repeating a similar statement before editing the differences. |
| Jump to specific line number | — | Cmd+L | Brings up a lightweight line-number field docked at the top of the editor; typing a number and hitting Return snaps straight to it, which is the fastest way to reach the exact line cited in a crash report or a compiler diagnostic without touching the mouse or the jump bar. |
Comment toggling (Cmd+/) adds or removes a leading // on the current line or every selected line at once, the fastest way to temporarily disable a block of code during debugging without deleting it outright and needing to retype it later.
Re-indent (Ctrl+I) automatically reformats indentation on the selected code (or the entire file, if nothing is selected) according to Xcode's configured style rules, commonly reached for after pasting code copied from a different source, a Stack Overflow answer, or another project entirely, where the original indentation style doesn't match your project's conventions.
Moving lines (Cmd+Option+[ and Cmd+Option+]) relocates the current line or selected block up or down by one position, letting you reorder statements or reorganize method bodies without a manual cut-and-paste operation, which especially at scale (moving a large block several positions) is meaningfully faster via repeated key presses than dragging with the mouse.
Code folding (Cmd+Option+Left/Right) collapses a function, class, or block down to a single summarized line showing just its signature, temporarily hiding implementation detail that isn't currently relevant — useful in a large file where you want to see the overall structure of several methods at once without each one's full body cluttering the screen, then expanding back open exactly the one you need to actually read or edit.
Beyond the core shortcuts already covered, Xcode's editor also supports multi-cursor-style editing through a feature called Multiple Cursors, letting you place several cursors at once (via Ctrl-clicking multiple locations, or selecting a symbol and using Edit > Find > Select All Occurrences of a highlighted term) and then type or edit at all of them simultaneously — useful for renaming several similarly-named local identifiers in one pass without invoking a full project-wide rename operation for something that's only relevant within a single small scope.
Xcode's editor is also deeply tied to its live syntax and type checking, meaning code-folding and re-indentation both respect Swift's actual syntax structure rather than treating the file as generic text — folding a function collapses exactly to its matching closing brace regardless of nested complexity inside, and re-indentation correctly accounts for Swift-specific constructs like multi-line closures and trailing closure syntax that a naive text-based indenter would likely get wrong. This syntax-awareness is part of why re-indenting pasted code from an external source in Xcode tends to produce cleaner results than a purely mechanical find-and-replace-based reformatting would.
Code completion, triggered automatically as you type or manually via Escape or Control+Space depending on version, works alongside these editing shortcuts constantly, and accepting a suggested completion with Tab or Return integrates smoothly with the move-line and fold shortcuts covered here since completed code can immediately be reordered or folded like any other code once inserted.
Duplicating the current line or selection creates an immediate copy directly below the original, a small but genuinely time-saving shortcut for the common pattern of writing several similar consecutive statements — declaring a handful of related properties, or building out a switch statement's cases — where duplicating an existing line and then editing just the parts that differ is faster than retyping the shared structure from scratch each time.
Jumping to a specific line number (Cmd+L) is the fastest way to navigate directly to an exact location referenced by a compiler error, a crash report's stack trace, or a code review comment citing a specific line — considerably faster than scrolling or visually scanning through a long file to find a line you already know the exact number of.