autopep8 with vim

时光总嘲笑我的痴心妄想 提交于 2019-12-04 02:28:29

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.

PhML

autopep8 is included into python-mode.

Call :PymodeLintAuto or map it:

" Automatically fix PEP8 errors in the current buffer:
noremap <F8> :PymodeLintAuto<CR>

You should be able to do something like this to run the current file through autopep8:

:!autopep8 -i expand("%")

If you want to do this automatically on write for every python file, you can add this to your vimrc:

if has("autocmd")
    autocmd BufWritePost *.py !autopep8 -i expand("%")
endif

The plugin didn't exist yet when the question has been submitted, but now there is vim-autopep8.

https://github.com/tell-k/vim-autopep8

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!