Automatically add newline on save in PyCharm?
Solution 1
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.
Solution 2
Newer PyCharm I believe (2020+):
Solution 3
PyCharm 2021.1
You can now find this setting here:
Settings
> Editor
> General
> Ensure every saved file ends with a line break
.
Solution 4
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
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:
But if you do a search, it finds it:
Hope it helps. Cheers!
![Markus Meskanen](https://i.stack.imgur.com/lgpuB.jpg?s=256&g=1)
Markus Meskanen
Updated on June 06, 2022Comments
-
Markus Meskanen about 2 years
PyCharm 5 complains of a missing newline at the end of the file:
How do I tell PyCharm to add the newline (if missing) automatically whenever I save a file?
-
dom96 over 7 yearsIs there a reason why this isn't enabled by default in PyCharm?
-
Bill Agee over 7 yearsSublime Text works this way too. My guess is the authors made the decision that surprising users who are unaware of the need for the trailing newline was worse than creating extra work for folks who want that behavior enabled in their editor.
-
radtek almost 7 yearsHmm I don't have an "Other" section in PyCharm Community Edition 2016.3. Will upgrade to the 2017 version and see if it has it.
-
dcoles almost 7 years"Other" is not a selection in the navigation tree, but rather a section divider in the right-hand pane of
Editor > General
. -
Adam Wildavsky almost 4 yearsIn Pycharm 2020.1.3 the setting is now "Ensure an empty line at the end of a file on Save".
-
Jarad about 3 yearsThis is the answer in 2021.