13

I would like to automatically reformat the code in a file whenever I save it. Is it possible to do it in PyCharm? So far, this is the only feature from Eclipse I cannot find in PyCharm.

If it is possible, can you please point me to the setting. I have gone through the preferences and looked around the Internet but am unable to find this setting (if it exists).

P.S. PyCharm devs: if this does not exist, it is at the top of my PyCharm wish list.

Krystian Cybulski
  • 10,789
  • 12
  • 67
  • 98

3 Answers3

9

JetBrains team is working on ability to have on-save-actions. You'll be able to record a macro of what actions need to happen when you hit Ctrl/Command+S. Not sure when this will be released though.

More information at http://youtrack.jetbrains.com/issue/IDEABKL-6722

iNazarV
  • 158
  • 1
  • 3
6

There's a plugin for that attached to http://youtrack.jetbrains.com/issue/IDEABKL-5806 The comments to the issue also explain why it hasn't been implemented as a core product feature.

yole
  • 92,896
  • 20
  • 260
  • 197
2

Jetbrains have implemented Actions on save in PyCharm.

Actions on Save page has been added to Settings (Preferences) in 2021.2. At the moment it contains the following options:

  • Reformat code (if VCS is used it has a choice Whole file <-> Changed lines)
  • Optimize imports
  • Rearrange code
  • Code cleanup (applies fixes from the code cleanup inspections)

Also, there's a bunch of 'actions on save' that have already been available in the previous IDE versions and could be configured on their own technology-specific pages in Settings. It's now possible to enable/disable these features right on the Actions on Save page without jumping to technology-specific page. Note, that all these options remain available on the technology-specific page and UI is in sync with Actions on Save page.

  • Run eslint --fix (provided by the JavaScript plugin)
  • Run Prettier (provided by the Prettier plugin)
  • Build project (only in the IDEs that support Java)
  • Run File Watchers (provided by the File Watchers plugin)
  • Upload to default server (provided by the FTP/SFTP Connectivity plugin)

IDE screenshot

For details check this link: https://youtrack.jetbrains.com/issue/IDEA-100701#focus=Comments-27-4985850.0-0

Cool Breeze
  • 738
  • 2
  • 10
  • 26