JavaFormatter Free Online Java Formatter
Tips

How to Indent Java Code in VS Code, IntelliJ, and Notepad++

2026-08-26 · 6 min read

You paste a Java snippet from Stack Overflow into your editor, hit save, and the indentation falls apart: three editors, three defaults, one inconsistent file. VS Code, IntelliJ IDEA, and Notepad++ each handle indentation differently, and until you align their settings, every file you touch picks up a new mix of tabs and spaces. This guide sets up auto-indent correctly in all three, then covers the cross-editor fix that keeps them from fighting each other.

Why Editors Keep Disagreeing

Indentation settings live in two places: the editor defaults and the per-project configuration. When a file is opened, some editors detect the existing style, others apply their own. A file edited in VS Code with tab indentation, then touched in IntelliJ with space indentation, ends up with both. The fix is never to rely on defaults. Configure the editor once, commit the configuration to the repository, and let the tooling enforce it.

VS Code: Format on Save in Two Minutes

Install the Extension Pack for Java. It ships the language server that understands Java syntax, which is what turns formatting from a guess into a real re-indent. Then open settings.json and add:

{
  "editor.formatOnSave": true,
  "editor.insertSpaces": true,
  "editor.tabSize": 4,
  "[java]": { "editor.defaultFormatter": "redhat.java" }
}

From now on, every save re-indents the file from the actual brace structure. To format once on demand, press Shift+Alt+F. This matches the behavior described in why indentation matters in Java: the tool, not your eyes, should own the whitespace.

IntelliJ IDEA: Reformat Code and Continuation Settings

IntelliJ formats Java out of the box. Press Ctrl+Alt+L to reformat the current file, or press it twice to open the dialog with options like Optimize Imports. Under Settings, Editor, Code Style, Java, Tabs and Indents you control the block indent (4 by default) and the continuation indent (8 by default), which decides how wrapped lines line up. If your team argues about those two numbers, that argument is exactly what line wrapping rules exist to settle.

To format on save, enable Actions on Save, Reformat code in settings. IntelliJ also supports per-scope formatting, so test code can follow different rules from production code if you want.

Notepad++: What It Can and Cannot Do

Notepad++ is a fast plain-text editor, but it has no Java parser. Select lines and press Tab or Shift+Tab to indent and unindent blocks. The Language menu applies Java syntax coloring. What it cannot do is look at your braces and rebuild the nesting, which is the actual fix for messy files. For that, copy the code into the online Java formatter, format, and paste it back. Ten seconds, zero configuration, and the result follows standard Java conventions.

The Cross-Editor Fix: .editorconfig

The EditorConfig file ends the settings battle. Put a file named .editorconfig in the repository root:

root = true

[*.java]
charset = utf-8
indent_style = space
indent_size = 4
insert_final_newline = true

IntelliJ reads it natively. VS Code needs the small EditorConfig extension. Any editor with support applies the same indentation rules to every file, so the editor you use stops mattering. This is the same principle behind settling spaces versus tabs: the decision belongs in one committed file, not in each person's preferences.

When You Cannot Touch the Settings

Shared machines, locked-down environments, code pasted into review tools: sometimes you format code where no configuration survives. That is the exact case the browser formatter covers. No install, no settings, works the same everywhere.

Frequently Asked Questions

How do I auto indent Java code in VS Code?

Install the Extension Pack for Java, open the file, and press Shift+Alt+F. To make it automatic, enable editor.formatOnSave in settings. The Java language server then reformats the file on every save.

What is the reformat shortcut in IntelliJ IDEA?

Ctrl+Alt+L on Windows and Linux, Cmd+Option+L on macOS. It applies your Code Style settings. Press it twice to open the reformat dialog with options like optimize imports and rearrange entries.

Can Notepad++ format Java code automatically?

Not with a real parser. Notepad++ can indent selected lines with Tab and Shift+Tab, but it has no Java syntax model. For a full reformat, paste the code into a free online Java formatter and copy the result back.

More Free Developer Tools

DevToolCore

JSON formatter, regex tester, Base64, URL encoder, and 15+ more dev tools.

FontGenerator

14 Unicode font styles for social media, Discord, and anywhere you paste text.

CyberFengShui

Online Feng Shui compass with flying star analysis and AI interpretation.