How to make IntelliJ IDEA insert a new line at every end of file?

 

https://stackoverflow.com/questions/16761227/how-to-make-intellij-idea-insert-a-new-line-at-every-end-of-file

Asked 11 years, 1 month ago
Modified 2 months ago
Viewed 138k times
431

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

656
 

Change your Editor settings:

Settings → Editor → General → Ensure line feed at file end on save

  • 4
    Is it possible to force only one? I.e. if there is 2 or more it deletes the extra newlines? 
    – Pylinux
     CommentedOct 25, 2014 at 11:28 
  •  
    It adds new line but the message is still there.(I am on Windows and use Checkstyle InteliJ plugin). 
    – Xelian
     CommentedMar 11, 2015 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
     CommentedMar 11, 2015 at 18:56
  • 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
     CommentedNov 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.   CommentedJan 2, 2017 at 1:57
 
116

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).   CommentedMay 5, 2023 at 17:47 
56

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. 
    – Tuno
     CommentedMay 18, 2018 at 4: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.   CommentedJun 15, 2018 at 19:30
  •  
    Note that in Android Studio it'll add a new line when you save any change, not on reformatting   CommentedNov 8, 2018 at 7:35
  •  
    @Maragues autosave for the rescue!   CommentedNov 8, 2018 at 17:58
  •  
    Great, tested with phpstorm (2019.2.1), after update.   CommentedAug 25, 2019 at 16:17
52

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

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

6

General -> Save Files For IntelliJ IDEA 2020.

Check the Bottom Right Corner: screenshot

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.

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

4

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

1

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

==========================

==========================

 

https://intellij-support.jetbrains.com/hc/en-us/community/posts/207071445-On-save-new-line-added-to-end-of-file-How-stop-this

 

On save new line added to end of file. How stop this? 

Followed by 14 people
 

I did a search in the forum for this but did not get any results back.  I looked in settings but there are so many I am not sure where to look.

When I save a file, PhpStorm adds a new line to the end of the file.  How do I stop this from happening?

Thanks.

9
18 comments
 

Let me add that if there is already a blank line at the end of the file it does not keep adding new lines.  If there is NO blank line then when I save the file a new blank line is added.  I actually want some files to have no blank line at the end.

Thanks.

0
 

Hi there,

Some tip first: If you do not know if some configuration option is available and where it located in Settings window, then use Search box in Settings window (the very first thing on left top corner) -- extremely helpful feature in such situations. If found nothing -- try to use another spelling/naming (sometimes the option can be named differently to what particular person may expect).

Back to your issue -- untick the following option: File | Settings | Editor --> Ensure blank line before end of file on Save
Try searching for "blank line" on Settings screen and see how it works.

14
 

You either enable it or disable it, it still adds that line on Windows 7 / Windows 10 and Ubuntu 14 machines. Why is that??

2
 

@Dragos Rosu

Works just fine here. Don't know other options that could do such thing (unless some another tool/plugin does it).

The only thing I can think of ... 1) do you have .editorconfig files in this project?.. and 2) Do you have EditorConfig Support plugin installed? Settings from such file will override similar option from IDE Settings/Preferences (the nature of such files).

10
 

That was it, thank you kindly @Andriy Bazanov!

I was hating Webstorm for a while now for this "hidden bug". Someone commited some editor configuration in our repository and that broke it up.

 

Thank you kindly!

2
 

In PHP Storm 2018.2.5 

File | Settings | Editor | General --> Other ---> Ensure line feed at file end on Save 

17
 

Note that the Editorconfig plugin may override this setting if you .editorconfig file has:

insert_final_newline = true
10
 

https://intellij-support.jetbrains.com/hc/en-us/community/posts/207071445/comments/207471069

 

FYI i tried finding this with the search bar in preferences and couldn't find it, that's why I came here. I searched newline, \n, and end of file. I didn't think about searching 'trailing space' or 'blank space'. Might be nice to be able to find it with 'newline' or 'end of file' as search params. would have saved me the search. Thanks for helping me find it though!

1
 

There is an entire section about new lines in php, this one is missing. 

The setting is unsearchable. In general phpstorm settings are hard to work with..

0
 

i tried the same thing, the only thing that really work was edit the .editorconfig file changing the key insert_final_newline to false 

0
 

@Mcriverag

the only thing that really work was edit the .editorconfig file changing the key insert_final_newline to false 

So you had it set to TRUE before? In such case it's expected behaviour: settings from .editorconfig file will take over the IDE own settings (the whole nature of such file).

So you either:

  • edit your .editorconfig file as per your needs (the best option as this will work in another IDE/editor as well + settings are part of the project files so another person will have easier time configuring his editor)
  • remove .editorconfig file so only IDE settings are in action
  • disable EditorConfig support in the IDE

 

0
 

They messed with this again and it is still not searchable (not even if you search for the exact text).

1
 

Ba Grootjen, the settings entries' names are not indexed as they are but rather by keywords, a query like "save file line break" finds the settings item.

2
 

If you want your files to be actual files according to POSIX standards, they need a line ending at the end of the file.

https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_206

3.206 Line

A sequence of zero or more non- <newline> characters plus a terminating <newline> character.

Note, however, that a single newline at the end of the file makes Storm make it look like there is a blank line at the end of the file. Use a hex editor to confirm that there is only a single newline at the end of the file. Maybe a better solution would be a gutter icon or a visual endOfFileNewLine character

0
 

This bug is STILL present in Windows. I had both unchecked 'Remove trailing blank lines at the end of saved files' and 'Ensure every saved file ends wit ha line break'. I'm using git-secret and it keeps adding newlines to the end of my secrets which makes them invalid.

0
 

@Sam Galbraith

This bug is STILL present in Windows.

A bug? Unlikely TBH.

 

Do you have .editorconfig in your project (or a folders above it)? Settings from here will override the IDE settings (the nature of such files).

If you have it and it has a rule for new lines at the end of the file then just add a rule to not to add it for your git-secret files.

0
 

It's definitely not a bug if a newline is added a the end of files.

Text files only follow the POSIX standard for text files if their very last character before the EOF is an EOL (normally newline) character. Files that do not follow that standard simply are not actually POSIX standard text files.

However, I have to re-enable "follow the standard ffs" setting whenever I reinstall a JetBrains IDE, because of threads like this one.

1
 

I agree the option should be enabled by default, even if you don't care about POSIX, do it to avoid this error ⛔ on github:

0
 
posted @ 2024-07-19 04:29  功夫 熊猫  阅读(17)  评论(0编辑  收藏  举报