autopep8 with vim

江枫思渺然 提交于 2019-12-21 09:14:11

问题


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

Thank you in advance.


回答1:


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.




回答2:


autopep8 is included into python-mode.

Call :PymodeLintAuto or map it:

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



回答3:


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



回答4:


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



来源:https://stackoverflow.com/questions/15285032/autopep8-with-vim

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