Vim and PEP 8 -- Style Guide for Python Code

26,521

Solution 1

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.

Solution 2

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

Solution 3

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/

Solution 4

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.

Solution 5

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

Share:
26,521
Grzegorz Wierzowiecki
Author by

Grzegorz Wierzowiecki

Updated on April 26, 2020

Comments

  • Grzegorz Wierzowiecki
    Grzegorz Wierzowiecki almost 4 years

    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).

  • Grzegorz Wierzowiecki
    Grzegorz Wierzowiecki over 10 years
    Good catch! I've encountered problems with indentation and similar stuff during editing mostly. Of course - "after editing" periodical stylechecks can be run, but having it during edit it more comfortable.
  • dannysauer
    dannysauer about 10 years
    With vim 7.4 (and probably earlier), a newer version of the file references in "EDIT2" is already included with vim by default. If you simply include if has("autocmd") filetype plugin indent on endif in your ~/.vimrc or /etc/vimrc, it automagically gets loaded on an out of the box vim. (that's three separate lines, BTW, and it's probably already in the default vimrc on most current systems)
  • WhyNotHugo
    WhyNotHugo over 9 years
    Syntastic + flake8 is really the way to go, and stupidly simple to set up.
  • medo
    medo almost 4 years
    pylint is still not working well with ale (use flake8), follow github.com/dense-analysis/ale/issues/2522 for updates.