Formatting & Indentation

How to auto-indent code on Mac

You can do this with Note My: press Return and the caret keeps your depth instead of dropping to column zero. Here is how to switch it on, which of the five switches controls what, and how to shape it around the way you write.

You press Return at the end of a line that is four levels deep, and the caret lands at column zero. So you press Tab four times, or hold the space bar, and by the third block you are indenting more than you are writing. What you want is an editor that starts the next line where the last one left off.

In Note My that behaviour is called Smart Editing. It is a single toggle in the toolbar covering three related habits: carrying your indentation onto the next line, closing brackets and quotes as you open them, and finishing HTML tags. This guide is about the first of those — what Return gives you, how to control it, and how to shape it around the way you write.

What you get when you press Return

With Smart Editing on, Return starts the next line where you were going to start it anyway. Our indentation engine does that work on your Mac — nothing is uploaded, there is no account, and the file never leaves the machine. In practice you get five things.

  • Your depth carries down. Whatever indentation the current line begins with — tabs, spaces, or the mix you inherited — appears again on the new line. You keep typing instead of reaching for Tab.
  • A level opens after an opening bracket. End a line with {, [ or ( and the caret lands one further indent unit in. You are inside a new block, so you start inside it.
  • A level opens after a trailing colon. A line ending in : behaves the same way as an opening bracket.
  • A pair expands onto three lines. With the matching closer sitting immediately after the caret, Return gives you the opener on one line, an indented blank line with the caret on it, and the closer on a third.
  • A selection is replaced as normal. With text selected, Return replaces it the ordinary way. Auto-indent stays out of that.

All five are ordinary editing, and they stay entirely yours. A single ⌘Z reverses an automatic indent exactly as it reverses something you typed by hand, ⇧⌘Z brings it back, and our macro recorder treats it like any other edit you make — so a shape you build every day can be recorded once and replayed with the indentation already where you want it.

One indent unit is, by default, four spaces. The width is adjustable from 1 to 12 in the Indentation sheet, and you can switch to tabs there.

Note

Typing follows the global indent width, so one setting governs every file you type in. To choose the width Return gives you, set it against the global default in the Indentation sheet.

Turn it on and check it is on

  1. Press the Smart Edit button in the toolbar

    It is the toggle towards the right-hand end of the bar, sitting between Guides and Settings. Hover it before you press and the tooltip names the current state in words, so you always know which state you are about to move to.

    The same command is on the Edit ▸ Smart Editing menu, and in Settings ▸ Editing.

  2. Confirm the latched state

    A latched toolbar toggle in Note My is drawn as a solid accent-coloured disc with a white glyph — not a faint outline. If the button looks filled, smart typing is live.

  3. Test it on a real line

    Put the caret at the end of a line that ends in { and press ↩. The caret should land one level in from the line above, not at column zero. Press Return again and the depth holds.

  4. Test the three-line expansion

    Type { — with bracket pairing on, the closing } appears immediately after the caret. Now press ↩. With the closer sitting right after the caret you get three lines: the opener, an indented empty line with the caret on it, and the closer back at the opener's depth. That is the shape you want for a function body.

The five switches

Smart Editing gives you five switches, all on in a fresh install, in Settings ▸ Editing. That is why the behaviour above is what you get from the first line you type, with nothing to set up first.

The five Smart Editing switches and what each one controls
SwitchWhat it governs
MasterThe whole feature. The toolbar toggle is this one. Turning it off greys out the other four.
ReturnCarrying indentation onto the next line, opening a level after { [ ( or a trailing colon, and the three-line expansion.
BracketsAuto-closing (), [] and {}, and typing over a closer.
QuotesAuto-closing "", '' and backticks.
TagsInserting the matching closing tag when you type > in an HTML document.

Because they are independent, you can keep the part you like and drop the part you do not. Auto-indent on Return with no bracket pairing is a common preference, and it is one switch away — turn Brackets off and leave the rest alone. Quotes is the one to drop if you write prose or shell one-liners where a lone apostrophe is normal. Turn the master off and the other four grey out immediately, so you can see at a glance which parts are inactive.

The trailing colon, in every language

A line ending in : opens a level, and it does so in every language. That is what makes Python comfortable straight away: if x:, def f(): and for row in rows: all start the next line in the right place, with nothing to configure. The same colon behaves that way everywhere else too — in Swift after case .some:, in C after a label, and in YAML after a key.

That is the shape a block wants, so a nested key, a mapping and a function body all land where you were going to put them. Where you want the next line level with the one above — a YAML key with a scalar value on the following line, say — delete the extra indentation and carry on typing. Nothing re-applies it behind you, so one correction is the end of it and the line stays as you set it.

Typing as you go, or re-indenting the whole file

These are two separate tools, and each is the right answer to a different question. Smart Editing keeps you moving forward; a re-indent tidies what is already there.

Smart Editing compared with re-indenting a whole document
Smart EditingRe-indent
When you get itAs you type, on the keystrokeOnce, when you press Apply
What it touchesThe line you are startingThe whole document
How you reach itToolbar toggle, Edit menu, SettingsThe toolbar Indent button
Language handlingThe same in every fileFollows the language you pick in the sheet
Brace styleLeaves your braces where you put themRewrites to K&R or Allman

Re-indenting opens a sheet with a language picker, a brace-style switch, indent controls and a live preview; nothing in your document changes until you press Apply. The toolbar Indent button opens it, and the preview card shows a sample block at whatever width and brace style you have selected, so you can try one, read the shape it produces, change your mind and try another before anything is committed. If you have pasted a block that arrived at the wrong depth, that is the tool for it; auto-indent only ever affects lines you are creating.

What stays under your control

Smart Editing stays out of the way while it works, which is what makes it comfortable to leave on all day:

  • It never reformats what you have already written. Auto-indent acts on the new line only. Lines above the caret are left exactly as you left them, and nothing changes behind you when you close a block.
  • It behaves the same in every file. Return works identically in Swift, Python, Ruby and a plain text file, so the keystroke you learn on one project is the keystroke you keep on the next. Language-specific shaping is what the Indent sheet is for, and it waits until you ask for it.
  • Typing follows your global indent width. One width applies wherever you are, so what Return inserts is predictable in every document you open. If you want tabs while you type, set the global default to tabs in the Indentation sheet and Return uses them from then on; a full re-indent can still hold Go to tabs and Python to four spaces when you want a file shaped per language.
  • Each kind of pairing is yours to keep or drop. Brackets, quotes and HTML tags have a switch each, so you can run auto-indent with bracket pairing and no quote pairing, or any other combination that suits the language you spend your day in.
  • Your words stay exactly as you typed them. No text-mutating assist is enabled anywhere in the editor: no auto-correction, no smart quotes, no dash substitution, no data detection. Nothing rewrites your words.
  • The toggle is always within reach. The toolbar button, the Edit ▸ Smart Editing menu item and Settings ▸ Editing all reach the same switch, so you can turn smart typing on for a code file and off for a paragraph of prose without leaving the window.

Smart typing also keeps its pace as a document grows. Return and the pairing behaviours are the same in a very large file as they are in a short one, and our large-file engine keeps a heavy document responsive while you work in it, so a generated file or a long log is somewhere you can carry on typing comfortably rather than somewhere you open and close again. However large it grows, it stays on your Mac.

Troubleshooting

Return still drops the caret to column zero

Check the master toggle first — hover the toolbar button and read the tooltip, which states the current state in words. If the master is on, the Return switch itself may be off in Settings ▸ Editing. The two are separate switches, so turn the Return one on and the caret keeps your depth from the next line onwards.

I want to be sure which state Smart Editing is in

Hover the toolbar button in the bar itself and the tooltip states the live setting in words. A latched toggle is also drawn as a solid accent-coloured disc with a white glyph, so a glance tells you the same thing from across the desk. For the full picture — the master and the four switches under it — open Settings ▸ Editing, where each one is listed with its own control.

Return accepts a code suggestion instead of making a new line

While the completion popup is visible it takes the Return key to accept the highlighted item. That is a separate feature with its own switch: accept-with-Return and accept-with-Tab can each be turned off in Settings ▸ Code Suggestions. Press ⎋ to dismiss the popup and Return behaves normally again.

My indentation is spaces but the project wants tabs

Open the Indentation sheet from the toolbar Indent button, select the global default in the language picker, and switch to tabs there. That is the setting typing follows, so Return inserts tabs from the next line onwards. Choosing a specific language in the same picker shapes what a full re-indent produces, which is how you keep one project on tabs and another on spaces.

Every line is indented but the depths are wrong

A new line starts at the depth of the line above it, so a wrong depth is carried down as faithfully as a right one. Once the structure has drifted, re-indent the document from the toolbar Indent button rather than nudging it line by line, then carry on typing with Smart Editing still on.

I want to see how deep each block goes

Turn on the Guides toggle in the toolbar — indentation guides are on by default, in any of ten styles chosen in Settings ▸ Guides. To see the actual characters, the Show Invisibles toggle draws spaces, tabs and line endings without altering the file.

Frequently asked questions

How do I turn auto-indent off without losing bracket closing?

Leave the master Smart Editing switch on and turn off only the Return switch in Settings ▸ Editing. The five switches are independent, so bracket pairing, quote pairing and HTML tag closing all continue.

Does auto-indent use tabs or spaces?

Whatever the global indent setting says. The default is four spaces. Change it in the Indentation sheet, reached from the toolbar Indent button, with the global default selected in the language picker.

Can I undo an automatic indent?

Yes. ⌘Z reverses it like any other edit, and ⇧⌘Z puts it back. An automatic indent is ordinary editing from end to end: our macro recorder treats it like anything else you type, so you can undo it, redo it, or fold it into a macro and get the same result each time you run it.

Why does my YAML file gain a level after every key?

A line ending in a colon opens a level in every language, not only in Python, which is what drops you straight into a nested mapping as you type it. Delete the extra indentation when the next line belongs at the same depth — Return only ever sets a starting position, so nothing re-applies it once you have corrected it. For a file of flat keys, turn the Return switch off for that session in Settings ▸ Editing and bracket pairing, quote pairing and tag closing all carry on as they were.

What is the quickest way to reach Smart Editing?

The toolbar toggle: one press, from wherever you are in the document, with the state visible in the bar afterwards. The same command sits on the Edit ▸ Smart Editing menu, and all five switches live in Settings ▸ Editing — so you flip the whole feature from the bar and tune the individual parts from Settings.

How does auto-indent behave in a very large file?

Exactly as it does in a small one. Return and pairing behave the same in a multi-megabyte file, and our large-file engine keeps the document responsive while you work in it, so you can open a heavy generated file and carry on editing at the same pace.