Automatically add newline on save in PyCharm?

Question:

PyCharm 5 complains of a missing newline at the end of the file:

PEP 8: No newline at end of file

How do I tell PyCharm to add the newline (if missing) automatically whenever I save a file?

Asked By: Markus Meskanen

||

Answers:

What I usually do is I create a macro to do three things:

  • Reformat code
  • Organise imports
  • Save all files

This will, indeed, add a newline at the end of each file.
Then I rebind the Save all shortcut (Cmd+S on Mac) to this macro.

You can read more about macros here. I also recommend using Navigate to Action to find actions you forget the hotkeys for.

Answered By: Alex Panov

This can be enabled in the Editor > General settings:

From the File menu open the Settings and select Editor > General. Under the Other section in the right-hand pane, check the Ensure an empty line at the end of a file on Save setting.

Ensure an empty line at the end of a file on Save (PyCharm PE 2020.1.3)

Answered By: dcoles

Here is how I found it in PyCharm Community Edition 2017.5. You can go to Preferences... and do a search for ensure line feed. Under Editor/General the Other option may not be visible as can be seen here:

Other is not visible

But if you do a search, it finds it:

Search finds this option

Hope it helps. Cheers!

Answered By: radtek

This should be a default feature but if it is not enabled:

go to settings > Editor > General

then you will see a checkbox option to select…

Answered By: YosemiteWarrior05

PyCharm 2021.1

You can now find this setting here:

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

Answered By: Kurt Bourbaki

Newer PyCharm I believe (2020+):

enter image description here

Answered By: Isan Hawke
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.