402

How do I make IntelliJ IDEA insert a new line at every end of file, so that GitHub doesn't complain for example?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
NightRa
  • 10,701
  • 3
  • 16
  • 22

10 Answers10

613

Change your Editor settings:

SettingsEditorGeneralEnsure line feed at file end on Save

dan1st
  • 12,568
  • 8
  • 34
  • 67
NightRa
  • 10,701
  • 3
  • 16
  • 22
  • 4
    Is it possible to force only one? I.e. if there is 2 or more it deletes the extra newlines? – Pylinux Oct 25 '14 at 11:28
  • I can't find this setting anymore in Idea 14, is it still possible to do this? – mrbrdo Nov 16 '14 at 11:24
  • 43
    @mrbrdo In intellij 14 it's ```Settings->Editor->General->Other->Ensure line feed at file end on save.``` – NightRa Nov 16 '14 at 15:43
  • It adds new line but the message is still there.(I am on Windows and use Checkstyle InteliJ plugin). – Xelian Mar 11 '15 at 10:16
  • @Xelian If the line is there, then there are bugs in the tools that highlight there isn't a new line. – NightRa Mar 11 '15 at 18:56
  • Is it possible to get IntelliJ IDEA to add a new line at the end of generated files as well? I'm having issues with TypeScript adding the sourceMap reference at the last line of JS files and not adding a new line after it. – Mats Aug 18 '15 at 14:27
  • 15
    the setting has moved in newer version: Settings -> Editor -> General -> Ensure line feed at file end on save – RomanKonz Dec 16 '15 at 08:18
  • @NightRa Why do we want to have new line at the end of a file? Is this some standard or smth? – Andriy Kryvtsun Aug 17 '16 at 19:09
  • 5
    Since 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. – ShatyUT Nov 07 '16 at 16:50
  • 1
    @AndriyKryvtsun I think this is \*nix-specific (like macOS and Linux), not the case in Windows. From [Wikipedia](https://en.wikipedia.org/wiki/Newline): "...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. – Franklin Yu Jan 02 '17 at 01:57
  • @AndriyKryvtsun, here is some more elaborated answers to your question: https://stackoverflow.com/q/4700312/369824 – L. Holanda Jun 15 '18 at 18:34
109

For MAC users:

Preferences > Editor > General > Ensure every saved file ends with a line break

maksim
  • 806
  • 2
  • 9
  • 16
ncubica
  • 8,169
  • 9
  • 54
  • 72
  • 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). – Top-Master May 05 '23 at 17:47
52

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 :)

sbedulin
  • 4,102
  • 24
  • 34
  • In WebStorm this will override the default IDE configuration if plugin enabled. – Tuno May 18 '18 at 04:55
  • 5
    Wow! 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. – L. Holanda Jun 15 '18 at 19:30
  • Note that in Android Studio it'll add a new line when you save any change, not on reformatting – Maragues Nov 08 '18 at 07:35
  • @Maragues autosave for the rescue! – sbedulin Nov 08 '18 at 17:58
  • Great, tested with phpstorm (2019.2.1), after update. – Arenas V. Aug 25 '19 at 16:17
  • I think the posted code snippet of the .editorconfig shall have an additional linefeed at the end. – Sven Döring Apr 27 '22 at 22:13
51

IntelliJ IDEA 2016.3

Approach 1

File > Settings... > Editor > General > Ensure line feed at file end on Save

File > Settings

Editor > General > Ensure

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

Help > Find Action

Ensure line feed

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
25

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 enter image description here

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 enter image description here

bhordupur
  • 872
  • 13
  • 14
6

General -> Save Files For IntelliJ IDEA 2020.

Check the Bottom Right Corner: screenshot

Community
  • 1
  • 1
akhilrawat001
  • 109
  • 1
  • 8
6

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

enter image description here

This should have been a comment, but I wanted to add the screenshot as well so wrote as an answer.

Abhishek Singh
  • 943
  • 8
  • 14
5

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.

Rider settings

Stelios Adamantidis
  • 1,866
  • 21
  • 36
1

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

intellij

suno3
  • 31
  • 3
0

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"

enter image description here

jascadev
  • 19
  • 1
  • 1
  • 5