在 PyCharm 中保存时自动添加换行符?

Automatically add newline on save in PyCharm?

PyCharm 5 抱怨文件末尾缺少换行符:

如何让 PyCharm 在我保存文件时自动添加换行符(如果缺少)?

我通常做的是创建一个宏来做三件事:

  • 重新格式化代码
  • 组织进口
  • 保存所有文件

这确实会在每个文件的末尾添加一个换行符。 然后我将 Save all 快捷方式(Mac 上的 Cmd+S)重新绑定到这个宏。

您可以阅读有关宏的更多信息 here. I also recommend using Navigate to Action 以查找忘记热键的操作。

这可以在 Editor > General 设置中启用:

从文件菜单打开 Settings 和 select Editor > General。在右侧窗格的 Other 部分下,检查 Ensure an empty line at the end of a file on Save 设置。

这是我在 PyCharm Community Edition 2017.5 中找到它的方式。您可以转到 Preferences... 并搜索 ensure line feed。在 Editor/General 下,Other 选项可能不可见,如下所示:

Other is not visible

但是如果您进行搜索,它会找到它:

Search finds this option

希望对您有所帮助。干杯!

这应该是默认功能,但如果未启用:

转到设置 > 编辑器 > 常规

然后您会看到 select...

的复选框选项

PyCharm 2021.1

您现在可以在此处找到此设置:

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

更新 PyCharm 我相信 (2020+):