In order to auto format your python code according to the guidelines defined by PEP8, you can use some sane default for editing python files and flake8 to check and fix possible errors in your scripts.
The default indentation rules do not comply with PEP8. In order to change them for python files, create a new file in ~/.vim/ftplugin/python.vim and place the following content in this file:
setlocal tabstop=4 setlocal softtabstop=4 setlocal shiftwidth=4 setlocal smarttab setlocal expandtab setlocal shiftround setlocal autoindent
PEP8 recommends that code should have a maximum line length of 79 characters. You can enforce this by adding the following line to the file:
However, it can be a good idea to have longer lines, I prefer to not have a hard wrap at 79. You can therefore opt to show a marker that helps you to avoid the character line limit by setting:
flake8 is a python module that you can install using your favourite package manager, e.g. using pip:
sudo pip install flake
vim-pathogen is a vim script that helps with managing the runtimepath to simplify the vim plugin management. You can install it by running the following command:
mkdir -p ~/.vim/autoload ~/.vim/bundle && \ curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim
then add the following to your .vimrc
Afterwards, install vim-flake8:
cd ~/.vim/bundle git clone https://github.com/nvie/vim-flake8
This is everything you need to do. Afterwards, you can open vim on a python file and press *< F7 >* to start flake and obtain information on your formatting.