How do I make IntelliJ IDEA insert a new line at every end of file, so that GitHub doesn't complain for example?
10 Answers
Change your Editor settings:
Settings
→ Editor
→ General
→ Ensure line feed at file end on Save
-
4Is it possible to force only one? I.e. if there is 2 or more it deletes the extra newlines?– PylinuxOct 25, 2014 at 11:28
-
43@mrbrdo In intellij 14 it's
Settings->Editor->General->Other->Ensure line feed at file end on save.
– NightRaNov 16, 2014 at 15:43 -
15the setting has moved in newer version: Settings -> Editor -> General -> Ensure line feed at file end on save Dec 16, 2015 at 8:18
-
5Since IntelliJ auto-saves, I don't get the new line unless I explicitly save the file. This doesn't quite solve it for me unless I'm missing something.– ShatyUTNov 7, 2016 at 16:50
-
1@AndriyKryvtsun I think this is *nix-specific (like macOS and Linux), not the case in Windows. From Wikipedia: "...newlines either separate lines or that they terminate lines." In *nix system they terminate lines, so it appears to some programs that there is a trailing blank line. Jan 2, 2017 at 1:57
For MAC users:
Preferences > Editor > General > Ensure every saved file ends with a line break
-
I want something similar, but for the first-line, is there any way? Currently the IDE does the opposite (and auto deletes empty lines from begin of file). May 5 at 17:47
Possible alternative with a number of handy features is EditorConfig
Just submit an .editorconfig
file to your repo
[*]
insert_final_newline = true
And it will work natively not only in Idea, but in all major IDEs (some require a plugin).
Now all team members would have same configuration, eol, eof, and no more tabs vs spaces :)
-
In WebStorm this will override the default IDE configuration if plugin enabled.– TunoMay 18, 2018 at 4:55
-
5Wow! Really like this answer. The new line before EOF is such a small details sometimes is not worth to explain to other people in the team who don't understand and don't care about it. This should be the answer of choice because. Other answers are more likely to be out of date when JetBrains rearrange the UI. This answer is more likely to be valid for good as EditorConfig is enabled by default on Intellij. Jun 15, 2018 at 19:30
-
Note that in Android Studio it'll add a new line when you save any change, not on reformatting– MaraguesNov 8, 2018 at 7:35
-
-
IntelliJ IDEA 2016.3
Approach 1
File > Settings... > Editor > General > Ensure line feed at file end on Save
Approach 2
Help > Find Action... (Ctrl+Shift+A) > type "Ensure line feed" > switch the toggle to ON (using the mouse click or Enter) for "Other: Ensure line feed at file end on Save" line
For Mac Users: IntelliJ Idea version 2020.2
Option1:
IntelliJ Idea -> Preferences -> General -> Ensure an empty line at the end of a file on save
Option2:
⬆️ + ⌘ + A or Or just click on Help from menu bar -> Find Action and then type Ensu and choose Ensure an empty line at the end of a file on save
-
8In current version (2021.1) it is called
"Ensure every saved file ends with a line break"
– SledJun 8, 2021 at 15:27 -
I was wondering about that. To me a "line break" did not necessarily equate to an extra line at the end of the file. Oct 15, 2021 at 15:57
-
1did not work for me - even though the option is checked, no new line gets inserted on save Mar 27, 2022 at 21:13
In latest versions of IntelliJ, the setting has been renamed to 'Ensure an empty line at the end of a file on save', and it has been moved under Setting>Editor>General>Save Files
This should have been a comment, but I wanted to add the screenshot as well so wrote as an answer.
As Rider (IDEA's cousin for .NET) is driving me crazy, this might be helpful for those writing C# as Ensure line feed at file end on Save
alone won't work. It needs
File → Settings → Editor → Code Style → C# → Line Breaks and Wrapping → Line feed at end of file
.
I don't remember changing it and I haven't imported any settings for sure, so I guess it's by default disabled.
-
1Thank you! I thought I was going crazy trying to get this to work right. Mar 24, 2022 at 22:17
With the Intellij version 2022.3.1
Preferences (cmd + ,
) > Editor > General > Ensure every saved file ends with a line break
Check, apply, and click ok
With the IntelliJ Idea version 2020.3: Go to File > Sttings > Editor > General > On Save
And then select/deselect "Ensure every saved file ends with a line break"