Python has a standard coding convention called PEP8. Coding according to this convention will ensure a unified code style and readability. Each editor usually has an extension tool that can automatically format the code according to PEP8.
In Emacs, automatic shaping can be realized as follows by using py-autopep8.
The standard py-autopep8 had the following features: --Automatically format the entire buffer --Automatically format the entire buffer when saving
However, with the above function, the code that you do not want to touch is automatically formatted. Because unintended commit history will be created Added a function that can be automatically shaped by region selection by forking.
Install autopep8
$ pip install autopep8
From the following, drop py-autopep8.el and place it in the load-path destination of elisp
https://github.com/fujimisakari/py-autopep8.el/blob/master/py-autopep8.el
Added to init.el
(require 'py-autopep8)
(define-key python-mode-map (kbd "C-c F") 'py-autopep8) ;Code formatting for the entire buffer
(define-key python-mode-map (kbd "C-c f") 'py-autopep8-region) ;Code formatting in the selection region
;;Automatically format the entire buffer when saving
(add-hook 'before-save-hook 'py-autopep8-before-save)
Recommended Posts