How to indent Java code on Mac
You can do this with Note My in a single pass. Here is where the controls live and what each one does to mixed tabs, brace placement, and lines carrying three statements at once.
You have a .java file that someone else indented, or that a merge indented for you. Tabs and spaces are mixed. Some braces open on the line, some on the next. A couple of lines carry three statements each. You want the file put right in one pass, and the rules you chose to still be there next week.
This guide covers where the re-indent controls live in Note My, what each Java option changes, and what a re-indent does to your code. All of it happens on your Mac: nothing is uploaded, there is no account, and the file never leaves the machine.
What the Indent button actually does
The toolbar carries an Indent item, drawn as a pair of curly braces. Three things about it are worth knowing before you press it.
First, it does not act immediately. It opens the Indentation sheet — a floating panel with a language picker, a brace-style switch, a preview card, indent controls and an Apply button. Nothing in your document changes until you press Apply.
Second, the toolbar is where it lives. The Indent button sits in the toolbar of every document window, in the same place each time, so the whole set of rules is one click away from the file in front of you — no digging through preferences to change a brace style and no context to lose.
Third, Apply does more than shift whitespace. Our indentation engine puts every block at its correct depth, moves braces to match the style you chose, and unpacks statements that share a line. That last part rewrites code, so read the result before you save.
Re-indent a Java file
-
Open the file
Press ⌘O, or drag the file from the Finder onto the tab bar. Our advanced language identifier picks Java for you, so the colouring and the Java controls are already pointed at the right language.
-
Save before you reformat
Press ⌘S. A re-indent changes the whole file at once, so give yourself a version to come back to. With the file saved, File ▸ Reload from Disk (⌘R) is a clean way back — it drops the reformatted text and hands you exactly the version that is on disk. Reloading never changes the file on disk, so you can lean on it as often as you like while you settle on the rules you want.
-
Click Indent in the toolbar
The Indentation sheet opens. Your document is untouched at this point, and ⎋ closes the sheet without doing anything.
-
Set the language picker to Java
The picker offers a global default plus the fifteen languages that get indentation rules of their own; Java is one of them. Choosing a language snaps the brace, width and tabs controls to the settings saved for it, so the sheet shows what Java will actually get.
-
Choose the brace style, the width, and the two Java switches
These are covered one at a time below. The preview card animates as you change them.
-
Press Apply
The whole document is reformatted. If it already matched what you asked for, nothing changes and the file stays clean. If you would rather have the version you started with, ⌘R brings your saved copy straight back — see the troubleshooting note below.
Brace style: same line or next line
Two brace styles are on offer, and each one applies to the whole file in a single pass:
| Style | What it does |
|---|---|
| Same line (K&R) | The opening brace stays on the line that introduced the block: class Main {. This is the default. |
| Next line (Allman) | The opening brace moves to a line of its own beneath the declaration. |
Brace style is stored per language, alongside indent width and tabs-versus-spaces, so every language keeps its own house style and remembers it between sessions. Java can stay on same-line braces while a C++ file in the next tab uses next-line, and neither setting disturbs the other.
If you are matching an existing codebase, copy whichever style is already there.
Case labels and annotations: the Java section
Below the general controls, the sheet shows a section belonging to the selected language. Java's carries two switches, and between them they settle the two arguments Java teams actually have.
Case labels. One governs whether the case labels inside a switch are indented a level into the switch body, or kept level with the switch keyword. Both conventions are common; you pick once and the whole file follows.
Annotations. The other governs annotation placement — whether an annotation such as @Override goes on its own line above the declaration, or sits beside it.
Check both switches before your first Apply. They are per-language policies, so whatever they are set to is what every Java file you format will get.
Indent width, tabs and spaces
The indent width defaults to 4 and the sheet accepts anything from 1 to 12. Tabs-versus-spaces defaults to spaces.
Two languages carry a unit lock, and the pair is asymmetric: Go is locked to tabs with that lock on by default, and Python to four spaces with its lock off by default. Java is unlocked. Whatever you set is what you get, which means a team standard of tabs is as easy to apply as one of spaces, and a width of two is as easy as a width of eight.
The Reset to global link appears whenever the selected language has overrides, and its scope is deliberately narrow: it returns brace style, indent width and tabs-versus-spaces to your global defaults, and stops there. Your language policies — including the two Java switches above — are left exactly as you set them, so putting the shared three back to standard never quietly undoes a decision you made about case labels or annotations. Change those in the same sheet whenever you want them changed.
Statements that share a line
This is the part people do not expect, so it is worth showing plainly. A line like:
if (ready) { start(); count++; }does not simply move left or right. It is split, so each statement lands on its own line at its own depth with the braces in your chosen style. That makes the file reviewable, which a one-liner never is — but it also means the change set after an Apply is larger than "whitespace only". Commit before you reformat a file you did not write.
Recovering code a comment marker swallowed
The sheet has a Recover damaged comments checkbox, off by default. It exists for one specific injury: a line where a // marker ended up in front of code that was meant to run, so a statement is silently commented out. Turn it on and those lines are split back apart, with the code returned to a line of its own.
Java is one of the thirteen languages this covers, and it is deliberately cautious rather than clever:
- It works on single-line comments, which is where this damage happens. Block comments are re-flowed inside their markers and never un-commented.
- Doc markers —
///,//!,/**,/*!— are always left alone, so Javadoc is never touched. - Anything ambiguous is left exactly as you wrote it. Nothing is un-commented on a hunch, so a real comment stays a real comment.
Leave it off for ordinary reformatting. Turn it on when you are cleaning up a file that came out of a bad merge.
What Java looks like before you reformat anything
Colour arrives without being asked for. Our syntax engine gives Java full colouring, so a file you have never opened before is readable straight away: class names, constants and your @Override annotations each stand apart from ordinary code, which makes an unfamiliar method easy to skim before you touch it. None of it is written into the file. Colour is on screen only, so it never turns up in what you commit.
While you type, ⌃Space offers completions — Java keywords and builtins, plus names you have already used — so a long identifier is two keystrokes rather than twenty. ⌃⇧Space shows a call tip: the signature of the call you are inside, with the argument you are on highlighted. Java is one of the languages that carry call tips, which keeps a long signature in front of you while you fill the arguments in.
If you are starting a file rather than fixing one, File ▸ New from Template and the toolbar Templates menu both reach our template library, which offers Main.java, capitalised the way the language expects.
Troubleshooting
I pressed Indent and nothing happened
Three possibilities, all benign. The sheet opens but you have not pressed Apply yet. Or the document already matched the settings you asked for, so there was nothing to change. Or the document is empty, in which case nothing happens.
Formatting a very large file is taking a while
A re-indent has no size limit: whatever you hand it, it will format. On a normal source file the result is instant. A generated file of many megabytes is a heavier request, so save first and let it finish before you carry on. Our large-file engine keeps a file that size smooth to open, scroll and edit, which is where most of your time with it goes anyway.
What the preview card is showing you
It holds a fixed if <condition> { … } snippet and redraws it as you change the width, tabs and brace style, so you can see the shape of a setting the moment you touch it — and read it before a single character of your own document has moved. Take it as a sample of the rules you are about to apply, settle the three controls against it, then read your own code after Apply.
The braces ended up in the wrong places
The cause is almost always a brace that was already unmatched before you started — the re-indent lays the file out faithfully to the structure it was handed, so a stray brace shows up as a stretch of code at the wrong depth. That makes it easy to spot. Reload from disk (⌘R) to get your saved version back, then track the brace down: double-clicking a brace folds its block, which is a quick way to see where a pair stops lining up. Fix the brace, press Apply again, and the file settles into the shape you asked for.
Getting back to the version you started with
With the file saved, File ▸ Reload from Disk (⌘R) is the route back: it drops the reformatted text and returns the version on disk, character for character. That is why the procedure above saves first — one keystroke before the reformat buys you a clean way out of it afterwards. A commit beforehand is the same insurance. Our auto-recovery safety net covers the other direction, bringing your open work back after a crash, so between the two you always have a path to the text you meant to keep.
Typing a case label indents one level too far
As you type, the line after a line ending in a colon starts one level deeper, in every language — so a Java case x: gains a level while you write. That is typing assistance, and it is not what your file will end up looking like. Re-indenting settles every case label to whatever the case-label switch says, so you can ignore the extra level while you work and let Apply tidy it.
Cleanup still reports mixed tabs and spaces
It will, and that is intended. Mixed tab/space indent is something our cleanup rules report to you, but reporting is all they do with it: you get the count, and your leading whitespace is left alone under every preset. The Indentation sheet is the one place that rewrites it.
What you can do once the rules are set
- Set the rules once and keep them. Brace style, indent width and tabs-versus-spaces are remembered for Java, so the next
.javafile you open already points at your team's standard. Opening the sheet and pressing Apply is the whole of it. - Format on your own terms. A re-indent happens when you open the sheet and press Apply, and at no other moment — not on save, not on paste, not on open. The file in front of you stays exactly as you left it until you ask for the change, which is what makes it safe to try a brace style and change your mind.
- Work through a folder at your own pace. Our find and replace searches a whole folder and groups the matches by file, so you can see which files carry the pattern you are chasing and open them straight from the list — then run the pass on each one and read what Apply did to it before you save.
- Read the change before you keep it. Our comparison view puts two files side by side, so a reformatted file and the copy you kept can be read against each other line for line — useful when the Apply has unpacked lines that were carrying several statements.
- Fold the file down to its shape. Double-click a brace and its block collapses, which turns a long class into a readable outline and makes the result of a re-indent easy to check at a glance.
- Keep the code to yourself. All of it happens on your Mac. Nothing is uploaded, there is no account and no sign-in, and your code never leaves the machine.
Frequently asked questions
Where do I find re-indent?
In the toolbar, on the Indent button drawn as a pair of curly braces. It sits in the same place in every document window, so it is one click away from whatever file you are in. Pressing it opens the Indentation sheet rather than changing anything, so you can look the settings over whenever you like and press ⎋ to leave the document untouched.
Does re-indenting change anything other than whitespace?
Yes. It moves opening braces to match your chosen style, applies the case-label and annotation policies, and splits statements that share a line onto separate lines. It changes structure, not only spacing. Commit or back up before running it on a file you did not write.
How do I work through the Java files in a project?
Open a file and press Apply. Your Java settings are already held in the sheet, so every file is a click and an Apply away from your team's standard, and you see what changed before anything is saved. To work out which files want the pass, our find and replace searches a whole folder and groups the matches by file, so you can line them up and open them straight from the list.
Can I use tabs for Java?
Yes. Only two languages carry a locked unit — Go to tabs, with that lock on by default, and Python to four spaces, with its lock off by default. Java has neither, so set tabs in the sheet and the whole file is rewritten with them. Brace style, width and tabs-versus-spaces are all stored per language, so Java can differ from C++ in the next tab.
Does the syntax colour get written into the .java file?
Your .java file holds exactly the characters you typed. Colour is on screen only, never in the text, so it cannot mark the document as edited and cannot leak into a saved file. It does survive a deliberate export to HTML, RTF or PDF, which is the one place it is meant to travel with the code.