How to collapse code blocks on Mac
You can do this with Note My. Fold the blocks you are not reading, keep the signatures on screen, and get the shape of a nine-hundred-line file back.
A long source file stops being readable long before it stops being manageable. You scroll past four functions to reach the fifth, lose your place, and the structure that was obvious when you wrote it is buried in its own body text. Collapsing the blocks you are not working on is the cheapest fix: hide the interiors, keep the signatures, and the file becomes an outline again.
This guide covers how to fold a block in Note My, the three ways to bring it back, what folding does to your document (nothing), and how to read a nine-hundred-line file by its shape once the interiors are out of the way.
Folding is a pointer gesture: double-click a brace to close a block, double-click it again to open it. Everything on this page is built on that one movement, so there is nothing to memorise before you start.
Fold a block
-
Double-click the brace
Put the pointer on an opening or closing brace and double-click it. Everything between that brace and its partner collapses out of sight, and the two braces stay where they are, so the line still reads as the start of a function rather than as a gap.
Aim for the brace character itself. Land a few pixels off and the double-click does the other thing a double-click does here — it selects that word and lights up every other occurrence of it in the document. That is worth having in its own right, and it tells you at a glance that your aim was a character or two out.
-
Use the gutter to find what is foldable
The line-number gutter carries a marker beside every foldable block, so in a file you did not write, that column shows you where the blocks begin without reading a line of it. The gutter also carries bookmark dots, and right-clicking a line there offers Add Bookmark to Line N or Remove Bookmark from Line N.
-
Fold inside a fold
Nesting works in the order you would want. Fold three inner blocks, then fold the function containing them, and unfold the parent again later: those three are still folded, exactly as you left them. You can work outwards and back in without losing the arrangement you built on the way.
-
Bring it back
Three gestures unfold, and any of them will do:
- Double-click the brace again. The same gesture toggles.
- Click where the folded text was. A click inside a collapsed interior expands it and stops there — it does not also drop your selection into the middle of the block.
- Click the fold arrow in the gutter. Useful when the brace itself is awkward to hit — a dense line, or a long one that has scrolled sideways because you have the Wrap toggle off.
Which pairs fold
Folding works on matched bracket pairs, and these three fold:
| Pair | Typically |
|---|---|
{ } | Function bodies, types, blocks, JSON objects, CSS rules |
( ) | Long argument lists and parameter lists spread over many lines |
[ ] | Array literals, JSON arrays, long collections |
Between them those three pairs cover most of what a working file is made of: a function body, a type, a JSON document, a CSS rule, an argument list that has outgrown the width of the window, an array literal that runs to forty lines. A Python or YAML file folds on the same principle — the multi-line lists, dictionaries and calls in it all carry real brackets, so all of them collapse — and for the levels marked out by indentation, indentation guides draw the nesting straight down the page. Guides are on by default, with ten styles to choose from in Settings ▸ Guides.
What folding does to your document
Nothing. That is worth saying plainly, because in some tools folding sits uncomfortably close to editing.
A fold does not delete text and it does not move it. Folding changes what you are looking at, not what you have: the hidden lines are still yours, still exactly as you left them, and still there when you unfold. It all happens on your Mac — nothing is uploaded, there is no account, and the file never leaves the machine.
Several practical things follow:
- The tab is never marked unsaved by a fold. Fold and unfold as freely as you like. Neither gesture counts as an edit, neither puts the unsaved dot on the tab, and neither is something undo will step back through — so folding your way around a file you have not otherwise touched leaves nothing to save.
- Line numbers and counts stay correct. ⌘L for Go to Line… still takes you to the right line, and the status-bar counters — Chars, Words, Spaces, Lines, Sel and Occ — do not change when you fold.
- A fold hides lines from your eyes and from nothing else. Folded text is still there in full, still written out when you save, and still turned up by our find and replace, so a search runs across the whole file whether the block around a match is open or closed. Fold freely in the middle of a find-and-replace pass; the results will not shift under you.
- Nothing about a fold is written to the file. Close the tab and the folds go with it, and anyone else opening the same file sees it fully expanded. For the lines you want to keep hold of while you work, add a bookmark alongside your folds: ⌘F2 toggles one, F2 and ⇧F2 step between them, and the dot sits in the gutter rather than in your text.
Folded code costs nothing to leave folded
Hiding a block is not a tidy-up the editor quietly charges you for later. Our syntax engine keeps the colouring accurate in the code you can still see, and typing stays responsive however much of the file you have folded away.
The practical advice, then, is to fold aggressively rather than sparingly. Work down the top level of a long file, closing each block as you pass it, until only the two or three functions in play are still open: you get a shorter file to scroll and a clearer one to think in at the same time. Unfold as you move on, or leave the rest folded for the session; there is no penalty either way, and nothing to undo when you are finished.
Move fast through a very large document
Not everything you open is source. A database dump, a day of server logs, an export that runs to millions of lines: our large-file engine keeps documents that size quick to open and smooth to scroll, and the status bar tells you when the file in front of you is one of the big ones.
In a file that big, navigation is what buys you the time. ⌘L takes you straight to a line number you already have from a compiler or a stack trace. Bookmarks hold the handful of places you keep coming back to, and F2 and ⇧F2 walk between them however many thousands of lines apart they sit. In a log, ⌥⌘E and ⌥⇧⌘E jump to the next and previous error, which turns a day of output into a short list of the moments worth reading.
For JSON, run Edit ▸ Format JSON first. It re-indents to two spaces and sorts keys, which puts every object and array brace on a line of its own — and gives you something to double-click.
Three different ways of getting around a long file
Folding is one of three tools, and they solve different problems.
| Tool | Best for | How |
|---|---|---|
| Folding | Seeing the shape of a file — hiding what you are not reading | Double-click a brace |
| Bookmarks | Returning to a handful of specific lines, repeatedly | ⌘F2 to toggle, F2 and ⇧F2 to step |
| Go to Line | Jumping to a line number you already have, from a compiler or a stack trace | ⌘L |
If what you want is "show me only the interesting lines", reach for bookmarks. Bookmark All Search Matches (⌥⌘B) marks every hit of a search in a single pass, and Copy Bookmarked Lines lifts exactly those lines out as text you can paste into a note, a message or a fresh tab.
Read a long file by its shape
Folding pays off most when you treat it as a way of reading rather than as tidying up. Open a file you have inherited, close the blocks from the top down, and what is left on screen is a list of the things the file can do: signatures, type names, the top-level keys of a configuration. That list fits on one screen where nine hundred lines did not, and you can decide what deserves your attention before you have read a single body.
A few habits make that quicker.
- Work from the outside in. Fold the outermost blocks first, find the two or three that matter, then open only those. Anything you folded inside them is still folded when you open the parent again, so the arrangement you build survives every trip in and out.
- Close what you have finished with. Reading a review or working through a merge, fold each block as you satisfy yourself about it. The file shortens as you go, and what stays open is precisely what you still owe attention to.
- Bookmark before you fold. ⌘F2 on the lines you know you will return to, and F2 steps between them whether the blocks around them are open or closed.
- Put two files side by side. Multi View gives each open tab its own pane, so a folded implementation and the interface it has to match can sit next to each other, each folded down to the part you are checking.
- Format first, fold second. Braces on lines of their own are easier to aim at and easier to read, so a formatting pass before a folding pass is rarely wasted work.
Troubleshooting
Double-clicking selects a word instead of folding
The double-click landed on text rather than on the bracket. Occurrence highlighting — every other copy of that word lighting up across the document — is the tell: it means you were a character or two off. Aim again, or aim at the closing brace instead, which usually sits alone on its own line with nothing beside it to hit by accident.
The brace will not fold at all
The usual cause is that the bracket has no partner — a stray opening brace in a half-written function, or an odd one somewhere above it that leaves the pair uneven. Balance the brackets and the fold appears; the closing brace you add takes a double-click straight away. In a file someone else wrote, the gutter is the quickest check of all: every block carrying a marker beside it has a complete pair and is ready to fold.
I am working in a Python or YAML file
Fold on the brackets. The multi-line lists, dictionaries, calls and literals all collapse exactly as they do anywhere else, and in a long configuration file that is usually most of the bulk. For the levels marked out by indentation, let the guides do the work: they run down each level of nesting and give you the same structural read at a glance.
I folded something and now I cannot find it
Look for the gutter marker beside the opening line, or click on the line where the block used to be — a click anywhere in a collapsed interior opens it and does nothing else, so whatever you had selected before the click is still selected after it. If a file has drifted into an arrangement you have lost track of, close the tab and open it again: it comes back fully expanded and identical to what is on disk, and you can fold your way back in from there.
My folds disappeared after a reload
Expected, and deliberate. Folds live in the session rather than in your text, so anything that loads the file again — File ▸ Reload from Disk (⌘R), or reopening it with a different encoding — hands you a clean, fully expanded document that matches the file on disk exactly. Fold the two or three blocks you care about again; it is a second's work, and it is what lets a fold be a way of looking at a file rather than a change to it.
Frequently asked questions
What is the quickest way to fold a block?
Put the pointer on a bracket and double-click it. That gesture, a click inside a collapsed block, and a click on the gutter arrow are the three ways in and out — all of them a single movement, all of them under the pointer you are already using to read.
How do I get an overview of an unfamiliar file?
The gutter tells you first: a marker sits beside every foldable block, so that column is already a map of where the functions begin. Then fold from the top down, closing each block as you work out what it does. By the time you reach the bottom you have both an understanding of the file and a file as short as its own contents page.
Does folding change the file on disk?
Your file is left exactly as it was. Folding hides text on screen and changes nothing else: the tab is not marked unsaved, the file on disk is untouched, and undo has nothing to reverse. It all happens on your Mac too — nothing is uploaded, there is no account, and there is no copy of your file anywhere but where you put it.
Can I fold a JSON object or an array?
Yes. Both { } and [ ] fold. Running Edit ▸ Format JSON first helps, because it re-indents to two spaces and sorts keys, putting the brackets somewhere you can reliably double-click them.
How do I read the structure of a Python file?
Two things work together. Fold the brackets — every multi-line list, dictionary and call collapses — and read the indented levels off the indentation guides, which run down each level of nesting and give you the same structural read at a glance. Guides are on by default, and there are ten styles to choose from in Settings ▸ Guides, starting with Dots.
Can I leave a file heavily folded all day?
Yes, and it is the way to work. A heavily folded file stays quick to type in, and our syntax engine keeps the colouring accurate in everything still on screen. Fold whatever you are not reading, leave it folded for as long as it suits you, and open it again when you need it.