使用formatprg = autopep8时如何设置正确的缩进?

DBe*_*nko 3 python vim pep8

设置以下选项: setlocal formatprg=autopep8\ -aa\ -

当我在可视模式中选择一行并按下时gq,autopep8对该行进行修改,但它也改变了该行的缩进:它默认为4个空格的缩进,无论该行的当前缩进级别(s) ).我希望它假设线条正确缩进,因此不要管它们.

同样,如果我设置--indent-size 8开关,autopep8它会将线条推到8个空格的缩进级别.请注意,如果视觉选择足够大以包含顶级缩进(即没有缩进的地方),则所有内容都会正确缩进.但在提交之前,我倾向于Visual Select较小的块.

DBe*_*nko 6

你需要付出代价--indent-size 0.所以我.vimrc有这条线:

" Use gq to clean up code
" Indent to 0 because otherwise it pushes selected lines to the given
" indent-size, no matter the current indent level of the line.
au FileType python setlocal formatprg=autopep8\ -aa\ --indent-size\ 0\ -
Run Code Online (Sandbox Code Playgroud)