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

标签 python vim pep8

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

当我在可视模式下选择一行并按下 gq 时,autopep8 对该行进行了修改,但它也改变了该行的缩进:它默认为 4 个空格的缩进,无论行的当前缩进级别如何。我希望它假设这些行缩进正确,所以不要管它们。

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

最佳答案

你需要给 --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\ -

关于python - 使用 formatprg=autopep8 时如何设置正确的缩进?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24449056/

相关文章:

用于 AWS S3 的 Python + Boto3 : Error - Bucket name must match the regex "^[a-zA-Z0-9.\-_]{1,255}$"

python - 如何删除空白的二维列表行?

python - 将 Plone 联系表工作到 main_template.pt

python - 在python中使用beautifulsoup获取链接名称和 "selecting"链接而不是限制?

search - vi 搜索复制粘贴搜索复制

vim 切换颜色方案的函数

vim - 删除单词和前面的空格

python - 如何修复 Vim 在 Python 中对长行的换行行为?

Python PEP8 约定