Python is whitespace dependent, and it can't be totally autoindented for that reason, but there are a lot of extra stuff that should be possible to do automatically to comply with PEP8.

Things like whitespaces around operators, two new lines before a class and one before a method, etc.

Is there any equalprg compatible utility for that? If not, how to program one?

  • PEP8 says: "But most importantly: know when to be inconsistent -- sometimes the style guide just doesn't apply. [...] Some other good reasons to ignore a particular guideline: 1. When applying the guideline would make the code less readable, even for someone who is used to reading code that follows this PEP. [...]" ... PEP8 are guidelines only. The universe is complex. Code is complex. You can't make rules for every possible scenario, so this is why PEP8 are guidelines, and not rules. The whole point of Python is readability, and especially because of that ... Feb 8 '15 at 5:39
  • ... exceptions to PEP8 are sometimes required... Using an automated tool to "fix" the code style can't do these exceptions, and is a flawed concept ... If you want PEP8-compliant code, then type PEP8 compliant code, and when it makes sense, make an exception. Feb 8 '15 at 5:41

Yes, vim-autopep8.

You'll need to pip install autopep8 as well.

You can then call :Autopep8 and it will fix most of the issues in your code. You can configure which issues to fix using either an inclusion list or an exclusion list, based on the errors in the Pep8 error codes.

It will also spit out a nice diff showing exactly what changed when you run it.

  • 1
    This is a link-only answer. Please provide more detail about how to use the plugin to accomplish the goal. Feb 8 '15 at 5:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.