Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

autopep8 with vim

Tags:

python

vim

pep8

Is there a way to automatically apply autopep8 to a file being edited in vim? I have the following vimrc.

Thank you in advance.

like image 602
user977828 Avatar asked Mar 08 '13 01:03

user977828


2 Answers

The correct way of using autopep8 is to rely on vim "formatprg" settings. So you add the line below to vimrc:

au FileType python setlocal formatprg=autopep8\ -

Now when you select lines in python and hit gq (the default mapping unless you remapped it). It will filter the lines through autopep8 and writes the nicely formatted version in place.

The setting above also make it work with vim objects and vim motions, so you could rerender a paragraph (well lines of python code with no blank line between) using gqap.

To do the whole file you could do gggqG

The hyphen '-' at the end of the command is required to make autopep8 read the lines from the standard in.

like image 195
Meitham Avatar answered Nov 11 '22 19:11

Meitham


autopep8 is included into python-mode.

Call :PymodeLintAuto or map it:

" Automatically fix PEP8 errors in the current buffer:
noremap <F8> :PymodeLintAuto<CR>
like image 8
PhML Avatar answered Nov 11 '22 19:11

PhML