Add a Newline at the End of Files in IntelliJ IDEA
Sometimes, saving a file in IntelliJ IDEA doesn’t automatically add a newline at the end, which can create problems with version control, development tools, and automated workflows. Luckily, IntelliJ IDEA allows us to configure files to always end with a newline, ensuring our projects are consistently formatted. This article explains why the final newline matters and shows how to configure IntelliJ IDEA to always insert one.
1. Why a Newline at the End of the File Matters
A newline at the end of a file is more than a stylistic choice. Text files are traditionally defined as a sequence of lines, each ending with a newline character. Including a newline ensures compatibility with different systems and tools.
In version control systems like Git, files that end with a newline produce cleaner diffs, making it easier to track actual changes. Many linters, formatters, and compilers also expect files to end with a newline, and omitting it can trigger warnings or cause unexpected behaviour in automated workflows.
Enforcing a newline at the end of every file promotes consistency across a team and eliminates unnecessary formatting-only changes in code reviews. This simple practice helps maintain cleaner repositories and smoother collaboration.
2. Enable “Ensure New Line at EOF” in IntelliJ IDEA
We can enable this feature directly through the IDE settings. IntelliJ IDEA provides a built-in option to automatically add a newline at the end of every file on save, ensuring consistent formatting and preventing potential issues. Below is a step-by-step configuration guide to set it up.
- Open IntelliJ IDEA.
- Go to Settings / Preferences:
- Windows :
File > Settings - macOS:
IntelliJ IDEA > Preferences
- Windows :
- Navigate to Editor > General.
- In the On Save section, enable:
- Ensure every saved file ends with a line break
Once this option is enabled, IntelliJ IDEA will automatically add a newline at the end of the file whenever you save it.
3. Apply the Rule Consistently with EditorConfig (Recommended)
To ensure consistent behaviour across your team and different editors, you can use EditorConfig to enforce a newline at the end of every file. IntelliJ IDEA supports EditorConfig out of the box, so no additional plugins are needed.
Creating or Opening an .editorconfig File
- In your project’s root directory, check if an
.editorconfigfile already exists. - If it doesn’t exist, create a new file named
.editorconfigin the root directory of your project. - Open the
.editorconfigfile in IntelliJ IDEA or any text editor to add or update configuration rules.
Example .editorconfig
root = true [*] end_of_line = lf insert_final_newline = true
Explanation of the .editorconfig Rules
root = true: This indicates that this.editorconfigfile is the top-most configuration file, so EditorConfig will stop searching parent directories for additional rules.[*]: The asterisk applies the following rules to all file types in the project. You can customise this for specific file extensions if needed.end_of_line = lf: Ensures that line endings are set to LF (Line Feed), which is common for Unix-based systems and helps maintain consistency across different platforms.insert_final_newline = true: This is the key setting that ensures every file ends with a newline character. When enabled, editors and IDEs will automatically add a newline at the end of a file if it is missing.
Using .editorconfig works across multiple IDEs and editors, not just IntelliJ IDEA, allowing formatting rules to be version-controlled for the entire team. It helps prevent accidental deviations from coding standards and ensures that all text files consistently end with a newline, avoiding potential issues with version control, linters, and automated tools.
4. Common Pitfalls and Tips
- Binary files: The setting applies to text files only, which is usually what you want.
- Generated files: If you don’t want formatting changes in generated sources, consider excluding them from
.editorconfigor version control. - Existing files: The newline is added on save, so older files will be fixed gradually as they are edited.
5. Conclusion
Ensuring a newline at the end of every file is a small change that delivers outsized benefits—cleaner diffs, better tool compatibility, and fewer formatting debates. With IntelliJ IDEA’s built-in “Ensure every saved file ends with a line break” option, and optional .editorconfig support, we can enforce this rule automatically and consistently across our projects.





