34
votes

Could you recommend, how to configure Vim to stick to :

PEP 8 -- Style Guide for Python Code

when editing python2/3 files, and only those (I'd like to leave configuration as it is for all other filetypes).

5

5 Answers

23
votes

As 2020

Using ale plus installing a linter pip3 install pylint is asynchronous and therefore probably the better idea. It does not block when you save, syntastic will block. It also works for other languages (like syntastic). It is based on the Language Server Protocol.


Old Answer (2014)

Using syntastic plus installing pip install flake8 gives you the best experience IMHO. syntastic is great, because it not only does pep8 checks for Python, but by installing other software it's really easy to add syntax checks and the like for other languages.

Old Answer (2013)

vim-flake8 is the better choice, I will try it :-)

Oldest Answer (2012)

There's also a little script called 'pep8' - https://github.com/vim-scripts/pep8

I have it configured on 8:

let g:pep8_map='<leader>8'

I really like it. It works really good.

16
votes

There's vim-flake8, which is most easily set up using vim-pathogen or Vundle.

11
votes

The answers are focusing on checking the style after source code has been written but the question seems to be about making vim stick to the pep8 style during editing itself.

The main problem I've had with vim and pep8 is indentation, which can be fixed using this vim script:

EDIT3: Fix the terrible English...

EDIT2: Whoops, wrong script! Fixed. http://www.vim.org/scripts/script.php?script_id=974

EDIT1:

The script has been forked on github:

https://github.com/hynek/vim-python-pep8-indent/

3
votes

I would recommend to use the inclusive klen/python-mode plugin.

Where you can use :PymodeLintAuto for PEP8 errors and warnings with every time you save your code.

1
votes

Here is a vimrc configuration file which I prefer to use. It takes care of all PEP8 configuration.