Pretty-Py allows you to format your Python code using standard tools, currently supports:
- yapf (default)
- autopep8
- black
- Unified interface for on the fly switching between different Python code formatters.
- Afaict all of the existing packages just replace your whole buffer which the new formatted content. This packages uses a smart diff mode to only make minimal changes to your buffer (adapted from go-mode.el). This has some a number of advantages, it's faster, it avoids font-locking your whole code a again and has better undo behavior.
- Supports blackd, the http daemon for black, avoiding repeated startup time. Hence this is the recommended option when using the
before-save-hook
.
- Currently you have to fetch it from github, melpa upload pending.
- Make sure your preferred formatter is installed. If it is your path
pretty-py
will find it, otherwise setpretty-py-{yapf,autopep8,black}-command
explicitly. - If you want to automatically format on every save, enable the minor mode
pretty-py-mode
from within yourpython-mode-hook
. - To format your buffer invoke
pretty-py-buffer
,pretty-py-buffer-yapf
,pretty-py-buffer-autopep8
orpretty-py-buffer-black
as required.
(defun my-python-mode-hook ()
(pretty-py-mode 1))
(use-package pretty-py
:init
(add-hook 'python-mode-hook #'my-python-mode-hook)
:config
(setq pretty-py-formatter 'black
pretty-py-use-blackd t
pretty-py-black-fast-flag t
pretty-py-black-line-length 79))