Home
Mastering the IntelliJ IDEA Code Format Shortcut for Windows, Mac, and Linux
Code readability is the backbone of sustainable software development. In IntelliJ IDEA, one of the most powerful Integrated Development Environments (IDEs) for Java, Kotlin, and many other languages, maintaining a clean codebase is simplified through its robust formatting engine. Whether you are working on a massive enterprise application or a small personal project, knowing how to trigger the code formatter efficiently can save hours of manual indentation and alignment.
The Essential IntelliJ IDEA Format Shortcuts
For those looking for an immediate answer to the most common query, the primary keyboard shortcuts for reformatting code in IntelliJ IDEA depend on the operating system:
- Windows and Linux:
Ctrl+Alt+L - macOS:
Option(⌥) +Command(⌘) +L
When these keys are pressed, IntelliJ IDEA applies the active code style settings to the current file. If no text is selected, the entire file is reformatted. If a specific block of code is highlighted, only that section will be adjusted.
How the Formatting Engine Works
The reformatting process in IntelliJ IDEA is not just about adding spaces or tabs. It is a sophisticated analysis of the Abstract Syntax Tree (AST) of your code. The IDE looks at your language-specific settings—such as how many spaces follow a comma, where braces are placed in a class definition, and how long a line can be before it wraps—and adjusts the text to match those rules perfectly.
Reformatting a Code Fragment
There are many scenarios where you might not want to reformat an entire file, especially when working on legacy code where a full-file reformat could lead to massive, unnecessary diffs in version control.
- Highlight the specific lines of code you want to clean up.
- Press
Ctrl+Alt+L(Windows/Linux) or⌥+⌘+L(macOS). - The IDE will adjust only the selected fragment, leaving the rest of the file untouched.
Reformatting the Entire File
If you have just finished writing a new class and the indentation is inconsistent, triggering the shortcut without any selection will bring the whole document into alignment. This is the fastest way to ensure that your new contribution meets the project's standards.
The Reformat File Dialog: Advanced Options
While the basic shortcut is powerful, sometimes you need more granular control over what happens during the formatting process. By adding the Shift key to the shortcut, you can open the Reformat File Dialog:
- Windows / Linux:
Ctrl+Alt+Shift+L - macOS:
⌥+⇧+⌘+L
This dialog provides several critical options that go beyond simple indentation:
Optimize Imports
This option removes unused import statements and organizes the remaining ones according to your project's rules (e.g., grouping java.* imports separately from third-party libraries). Keeping imports clean prevents class name collisions and reduces overhead.
Rearrange Entries
Based on your Code Style settings, this will reorder methods, fields, and inner classes. For example, you can configure the IDE to always place private fields at the top of a class and public methods below them.
Code Cleanup
This runs a set of inspections that can automatically fix common issues, such as replacing a traditional for-loop with a stream API call (in Java) or removing redundant qualifiers.
Only Changed Lines
If your project is under Version Control (like Git), this is perhaps the most useful feature. It allows you to reformat only the lines you have modified since the last commit. This keeps your Pull Requests focused on functional changes rather than cosmetic ones.
Automating Code Formatting
Relying on memory to press a shortcut every time you finish a line of code is inefficient. Modern development workflows often involve automation. IntelliJ IDEA offers two primary ways to automate formatting.
Reformat Code on Save
One of the most requested features by developers moving from editors like VS Code is "Format on Save." IntelliJ IDEA implemented this through the "Actions on Save" menu.
- Open Settings (Windows/Linux:
Ctrl+Alt+S) or Settings (macOS:⌘+,). - Navigate to Tools > Actions on Save.
- Check the box for Reformat code.
- (Optional) Click Configure scope to exclude certain files, such as generated code or large JSON files, from being reformatted automatically.
- (Optional) Select Optimize imports to keep your imports clean every time you save.
Once enabled, every time you press Ctrl + S or the IDE performs an autosave, your code will be perfectly formatted.
Reformat Code on Commit
To ensure that no unformatted code ever reaches your shared repository, you can trigger the formatter during the Git commit process.
- Open the Commit tool window (
Alt+0or⌘+0). - Click the Settings (gear icon) in the commit dialog.
- Under the Commit Checks section, enable Reformat code.
- You can also enable Optimize imports and Cleanup here.
Now, every time you commit changes, IntelliJ will run the formatter on the staged files, ensuring a consistent style across the entire team.
Distinguishing Between Formatting and Indenting
New users often confuse "Reformat Code" with "Auto-Indent Lines." While they seem similar, they serve different purposes.
- Reformat Code (
Ctrl+Alt+L): Applies the full set of code style rules, including wrapping, spacing around operators, and brace placement. - Auto-Indent Lines (
Ctrl+Alt+I): Only fixes the leading white space of a line to match the surrounding context. It does not change the internal structure of the code line.
If your code is mostly correct but the indentation got messed up during a copy-paste operation, Ctrl + Alt + I is a lighter, faster alternative.
Customizing the Code Style
The formatting shortcut is only as good as the rules it follows. You can customize these rules to match your personal preference or your company's style guide.
- Go to Settings > Editor > Code Style.
- Select your programming language (e.g., Java, JavaScript, Python).
- Here you can modify:
- Tabs and Indents: Choose between tabs and spaces and set the indent size (commonly 2 or 4).
- Spaces: Control exactly where spaces are placed (e.g., before parentheses, after keywords).
- Wrapping and Braces: Define when a line should be broken and whether braces should appear on the same line or a new line.
- Blank Lines: Determine how many empty lines should exist between methods or classes.
Using EditorConfig for Team Consistency
Maintaining a consistent style across a team of developers using different IDEs can be difficult. IntelliJ IDEA supports .editorconfig files, which allow you to define code styles in a plain-text file stored in your root directory.
When an .editorconfig file is present, IntelliJ IDEA will prioritize its settings over the IDE's internal configuration. This ensures that everyone—whether they use IntelliJ, VS Code, or Sublime Text—applies the same basic indentation and formatting rules.
Excluding Specific Code from Formatting
Sometimes, you have a specific block of code where the manual formatting is intentional—perhaps a complex matrix calculation or a DSL where standard formatting makes it harder to read. You can tell IntelliJ's formatter to ignore these sections.
Using Formatter Markers
- Go to Settings > Editor > Code Style.
- On the Formatter tab, enable the option Turn formatter on/off with markers in code comments.
- In your code, wrap the section like this:
-
Topic: jetbrains.com/idea blog.jetbrains.com/idea @intellijidea Windows & Linux keymap &Default macOS keymaphttps://resources.jetbrains.com/storage/products/intellij-idea/docs/IntelliJIDEA_ReferenceCard.pdf
-
Topic: IntelliJ IDEA DEFAULT KEYMAPhttps://pleiades.io/sites/willbrains.jp/keymap/pdf/shortcut_intellij_windows.pdf
-
Topic: Reformat code | IntelliJ IDEA Documentationhttps://www.jetbrains.com/help/idea/2025.2/reformat-and-rearrange-code.html?keymap=Windows