Formatting by Language

How to indent Swift code on Mac

You can do this with Note My in one pass. Here is how to put right a Swift file with mixed tabs, crammed statements and braces in three places — and how to set the Swift rules once so it stays that way.

Swift files go crooked in ordinary ways. Code arrives pasted from a web page with four-space indentation on top of your tabs. A merge leaves a block one level too deep. Something collapses two statements onto the same line. The compiler does not care; you do, because you have to read it.

This guide covers what re-indenting a .swift file in Note My gives you, how to reach it, which Swift options you get, and which settings are stored against Swift itself.

What the Indent button actually does

The Indent button in the toolbar does not reformat anything when you click it. It opens the Indentation sheet, and the document is untouched until you press Apply. That is deliberate: you settle the style first, see it in the preview, and commit only when it is what you want.

When you do apply it, our indentation engine lays the whole file out again so braces, blocks and statements sit at the depth they belong at, using your brace style, your indent width and the Swift policies. You get two things, not one:

  • It re-indents. Every line ends up at the depth its structure says it belongs at, in your unit.
  • It unpacks statements that share a line. Two statements crammed onto one line come back out onto lines of their own. This is the part that turns collapsed code back into something readable.

Swift has conventions of its own in the sheet, as do C, C++, JavaScript, TypeScript, Go, Kotlin, Rust, PHP, CSS, Java, Python, Ruby, Shell and HTML. Our advanced language identifier picks Swift up for you when you open the file, so the result follows Swift's brace and block conventions rather than something generic, and you do not have to tell it what it is looking at.

Note

Everything here is built in. Nothing is downloaded and no formatter has to be installed. It all happens on your Mac: nothing is uploaded, there is no account, and your source never leaves the machine to be formatted.

Re-indent a Swift file

  1. Save the file first

    Press ⌘S. Re-indenting changes the whole file at once, so a saved version on disk behind it is the clearest safety net. Our auto-recovery safety net covers the unsaved case, but a save is the version you can return to on purpose.

  2. Open the Indentation sheet from the toolbar

    Click Indent in the toolbar — the braces glyph. A floating panel opens with the language picker, a brace-style switch, a live preview card, the indent controls, a comment-recovery toggle and a Swift-specific section. The toolbar button is the way in, one click from wherever you are in the document.

  3. Confirm the language is Swift

    The picker offers sixteen options: a global default plus the fifteen languages with rules of their own. Our advanced language identifier will normally have Swift selected for you already, but check it — the settings you change below are stored against whatever is selected here. Changing the selection snaps the controls to that language's stored values.

  4. Set the brace style

    Two styles exist: same line (K&R — the opening brace stays on the line that opens the block) and next line (Allman — it drops to its own line). Same line is the default, and it is what almost all Swift code uses.

  5. Set the width, and tabs or spaces

    Indent width defaults to 4 and the sheet accepts 1 to 12. Spaces are the default. Swift takes either, so you can match whatever your team has settled on — four spaces, two, or tabs. Go and Python have locks of their own if you would rather one of those languages were held to a single convention; a Swift file stays yours to set.

  6. Read the preview, then Apply

    The preview card redraws as you change the controls, with the braces tinted so the style change is obvious. It shows a short built-in if snippet, so you can read your brace style and width at a glance and settle on them before anything touches the file.

    Press ↩ to apply, or ⎋ to cancel. Applying marks the tab with a leading *; the file on disk does not change until you save it.

The Swift-specific settings

Below the shared controls, the sheet shows a section that changes with the selected language.

Swift-specific indentation policies and their defaults
SettingOptionsDefault
Brace styleSame line (K&R) · Next line (Allman)Same line
Indent width1–124
Tabs or spacesEitherSpaces
Trailing commasPreserve · Add · RemovePreserve
Access modifiers on their own lineOn · OffOff
Parameter-list wrap columnA column number, or 00 (off)

Trailing commas are left alone unless you ask. Preserve means no comma is added to the last element of a collection or a parameter list, and none is taken away. Choose Add or Remove only if you have a house rule.

Column limits default to zero, and zero means off. The parameter-list wrap does nothing until you give it a number. That is true of every column limit in the sheet, in every language — nothing wraps by default.

Comments: your documentation comes through intact

Doc comments are load-bearing in Swift, and they are treated that way. ///, //!, /** and /*! markers are always left alone — a documentation comment is never split back into code. Block comments are re-flowed inside their markers, never un-commented.

Recover damaged comments is the checkbox for the other problem: a line where a comment marker has swallowed real code, so that // Preserve state let x = 1 let y = 2 is one comment rather than a comment and two statements. Switched on, that line comes back apart into the comment and the two statements it was hiding. Swift is one of the languages it covers.

It is off by default and deliberately cautious when on: single-line comments only, and anything it cannot take apart cleanly is left exactly as you wrote it. Leave it off for normal work; turn it on for a file a bad paste has damaged, apply, then turn it off again.

Swift's nested block comments — /* inside /* — survive intact, so a commented-out block that itself contains a comment stays commented out. Our hidden-character scanner treats a Swift file with the same care: protect authored text is on by default, so a curly quote inside a string literal or a dash inside a comment stays exactly as you typed it, while the status-bar pill goes on reporting what genuinely is out of place. You are told what is worth a look; you decide what to do about it.

How your Swift settings are remembered

This is the detail worth knowing before you set anything. Three settings are stored per language: brace style, indent width, and tabs-or-spaces. Set those with Swift selected and they apply to Swift alone, so the way you like your Swift laid out waits for you in every .swift file you open afterwards, in this window and the next one.

The Reset to global link appears once Swift has settings of its own, and it is more precise than it sounds: it clears those three and leaves every other choice in the sheet exactly as you made it. A trailing-comma policy, an access-modifier preference or a wrap column you set deliberately is still set afterwards, so putting the basics back to the default costs you none of the finer work. Read the Swift section once after a reset and you will see your own choices sitting there.

Keeping it tidy as you type

Re-indenting is the repair. Smart editing is the prevention, and it is on by default. With the Smart Edit toolbar toggle latched:

  • Return carries your indentation forward. The new line starts at the depth of the one you left, with one extra unit after a trailing {, [, ( or :. If the matching closer is the very next character, the pair opens onto three lines with the caret in the middle, ready for the body.
  • Six pairs auto-close: (), [], {}, "", '' and backticks. The closer appears as you open the pair, so a nested closure or a dictionary literal stays balanced while you write it.
  • Typing a closer over an auto-inserted one just moves the caret past it. You do not end up with two closers, and undo has nothing extra to step back through.
  • Quotes are suppressed straight after a letter or a digit, which keeps apostrophes from turning into pairs.

To see the structure rather than fix it, the Guides toolbar toggle draws a marker at each level of indentation. Guides are on by default, and ten styles are available in Settings ▸ Guides, Dots being the default.

Troubleshooting

I clicked Indent and nothing happened

Clicking Indent opens the sheet; it never formats on the click. If you applied and the document still looks the same, the result matched what you already had — which on an already-tidy file is the correct answer. An empty document changes nothing, silently.

The result put braces somewhere unexpected

Re-indenting takes your braces at face value, which makes it a fast way to find an unbalanced one. The first line that lands at a depth you did not expect is where the braces stopped matching, so look just above it for the brace that is missing or the one too many. Fix that, re-indent again, and the rest of the file falls into place.

Re-indenting a very large file takes a moment

There is no size limit on a re-indent, so a Swift file of any length goes through in a single pass. On an ordinary source file the result is there at once; on a very large one, give Apply a moment and the whole document comes back laid out in one piece rather than in instalments, so save first and let it finish. Opening, scrolling and searching a file that size stays quick, thanks to our large-file engine.

Typing still inserts the old width after I changed it

Auto-indent while you type follows the global width and tabs-or-spaces setting. Select the global default in the picker, set the width you want there, and typing and re-indenting will agree from then on.

A colon at the end of a line adds a level I did not want

Auto-indent adds one unit after any trailing :, in every language. In Swift that fires on a case x: inside a switch. Delete the extra unit, or switch off the Return behaviour in Settings ▸ Editing.

The file is still a mix of tabs and spaces

Re-indenting normalises the leading whitespace on the lines it touches, so the mixture goes away rather than being reported to you. To have it listed instead, Clean ▸ Clean Invisible / Special Characters… puts Mixed tab/space indent among its twenty-one categories, with a line and column for each finding. To simply see what you have, turn on Show Invisibles: spaces draw as · and tabs as →.

Where this earns its place

Re-indenting is a whole-document pass, and that is the point of it: one pass puts every line in the file at the depth its structure asks for, whether the mess arrived from a merge, a paste or a long afternoon. A few jobs it handles well:

  • Code pasted from a web page. Four-space indentation dropped into a tabbed file, or the other way round, comes out in your unit throughout. Paste always arrives as plain text, so nothing else rides in with it.
  • A block left one level too deep by a merge. A block that has drifted lands back at the depth its structure asks for, so you neither select it nor count units to bring it home.
  • Statements crammed onto one line. A minified or badly wrapped file comes back out onto one statement per line, which is usually the difference between reading it and rewriting it.
  • Matching a house style. Set the brace style, width and tabs-or-spaces with Swift selected once, and every Swift file you open afterwards is a click and a keystroke away from that style.
  • A file a bad paste has damaged. Turn on comment recovery, apply, turn it off again, and the code a comment marker swallowed is back on its own lines.

The toolbar keeps the two structural tools side by side: Indent to put the file right, and Guides next to it to read the levels as you work. Folding is in the text itself — double-click a brace and everything between it and its partner collapses, or click the fold arrow in the line-number gutter — so the parts you are not reading move out of your way while you follow the ones you are. Between them that is what you want when you are moving through an unfamiliar file rather than writing a new one.

For the whitespace you cannot see, Clean ▸ Clean Invisible / Special Characters… lists what a file is carrying across twenty-one categories, with a line and column against each finding, so you can review a file before you commit it as well as after you have tidied it.

Frequently asked questions

What is the quickest way to re-indent a Swift file?

Click Indent in the toolbar — the braces glyph — and the sheet opens with your Swift settings already in place. From there it is two keys: ↩ applies, ⎋ cancels. On a file you have already set up, that is a click and a keystroke.

Does re-indenting change the file on disk straight away?

Applying changes the document in front of you and marks the tab with a leading *. The file on disk stays as it was until you save with ⌘S, so you can read the result, keep it or step it back, and only then commit it.

Can I set four spaces for Swift and tabs for another language?

Yes. Width, tabs-or-spaces and brace style are the three settings genuinely stored per language. Select Swift in the picker, set them, and they apply to Swift files only.

What does "Reset to global" actually reset?

The three settings held for Swift: brace style, indent width and tabs-or-spaces. Every other choice in the sheet — the trailing-comma policy, access-modifier placement, the column limits — is left exactly as you set it, so returning the basics to the default keeps all the finer work you have done in the Swift section.

Will it rewrite my documentation comments?

Your documentation comes through intact. ///, //!, /** and /*! are always left alone, and block comments are re-flowed inside their markers rather than un-commented, so a doc block reads exactly as you wrote it after the pass.

Do I need to install a formatter or a plugin first?

Everything on this page is built in and ready the moment you open a .swift file. It all runs on your Mac, offline, on macOS 13.0 or later — nothing is uploaded, there is no account, and the file never leaves the machine.