Automatically format Python code into PEP8-compliant code with Emacs

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.

py-autopep8.gif

About the function of 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.

Introduction method

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

Automatically format Python code into PEP8-compliant code with Emacs
Automatically paste images into PowerPoint materials with python + α
I made an action to automatically format python code
Automatically translate DeepL into English with Python and Selenium
Get country code with python
Python with VS Code (Windows 10)
Debug Python with VS Code
Format json with Vim (with python)
String format with Python% operator
Document Python code with Doxygen
[Python] GUI for inserting TeX format into PowerPoint with 2 clicks [PowerPoint]
Complement python with emacs using company-jedi
Automatically build Python documentation with Sphinx
Install python with mac vs code
[Python] Format when to_csv with pandas
INSERT into MySQL with Python [For beginners]
Automatically create Python API documentation with Sphinx
Put protocol buffers into sqlite with python
Automatically generate Python Docstring Comment in Emacs
Automatically aggregate JCG deck distribution with Python
[Python] Automatically operate the browser with Selenium
Make Python scripts into Windows-executable .exes with Pyinstaller
Automatically search and download YouTube videos with Python
Automatically check Python scripts with GitHub + Travis-CI + pycodestyle
Build Python development environment with Visual Studio Code
Python (Windows 10) Virtual Environment / Package with VS Code
Debug with VS Code using boost python numpy
[Automation] Convert Python code into an exe file
Try to automatically generate Python documents with Sphinx
Python code that removes contiguous spaces into one
Note for formatting numbers with python format function
Use Python in Anaconda environment with VS Code
Create a Photoshop format file (.psd) with python
Convert the character code of the file with Python3
Static analysis of Python code with GitLab CI
Make JSON into CSV with Python from Splunk
[Blender x Python] Think of code with symbols
Make your Python environment "easy" with VS Code
Build a python execution environment with VS Code
Convert strings to character-by-character list format with python
FizzBuzz with Python3
Python string format
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Play with 2016-Python
python character code
Tested with Python
python starts with ()
[Python] Automatically translate PDF with DeepL while keeping the original format. [Windows / Word required]
with syntax (Python)
[Python] Algorithm-aware code
Bingo with python
Zundokokiyoshi with python
Python string format
format in python
Excel with Python
Microcomputer with Python
Cast with python