Is there a way to automatically apply autopep8 to a file being edited in vim? I have the following vimrc.
Thank you in advance.
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.
autopep8 is included into python-mode.
Call :PymodeLintAuto or map it:
" Automatically fix PEP8 errors in the current buffer:
noremap <F8> :PymodeLintAuto<CR>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With