Automatically add newline on save in PyCharm?

PythonNewlinePycharm

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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)

Solution 2 - Python

Newer PyCharm I believe (2020+):

enter image description here

Solution 3 - Python

PyCharm 2021.1

You can now find this setting here:

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

Solution 4 - Python

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.

Solution 5 - Python

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!

Solution 6 - Python

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...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarkus MeskanenView Question on Stackoverflow
Solution 1 - PythondcolesView Answer on Stackoverflow
Solution 2 - PythonIsan HawkeView Answer on Stackoverflow
Solution 3 - PythonKurt BourbakiView Answer on Stackoverflow
Solution 4 - PythonAlex PanovView Answer on Stackoverflow
Solution 5 - PythonradtekView Answer on Stackoverflow
Solution 6 - PythonYosemiteWarrior05View Answer on Stackoverflow