Vim Python 缩进不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2011589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Vim Python indentation not working?
提问by CarpeNoctem
I have Vim7 (enhanced) on CentOS 5, and it comes with all the usual Vim plugins/scripts ready to go.
我在 CentOS 5 上安装了Vim7(增强版),它附带了所有常用的 Vim 插件/脚本。
$ find /usr/share/vim/vim70/ -name \*python\*
/usr/share/vim/vim70/syntax/python.vim
/usr/share/vim/vim70/ftplugin/python.vim
/usr/share/vim/vim70/indent/python.vim
/usr/share/vim/vim70/autoload/pythoncomplete.vim
I would think that when opening a file ending in .py (vim file.py
) it would automatically load these plugins, but I am not sure that is the case. What I want is:
我认为当打开以 .py ( vim file.py
)结尾的文件时,它会自动加载这些插件,但我不确定是否是这种情况。我想要的是:
Press TABand receive four spaces. Auto indent next line for suites, conditionals, etc.
按TAB并接收四个空格。套件、条件等的下一行自动缩进。
I have this working by explicitly setting tabstop, shiftwidth, etc. in my .vimrc file. Isn't this what the above Pythonfiles are for? Why do I have to set these things in my .vimrc
? How do I get these features from the Vim plugins instead?
我通过在我的 .vimrc 文件中显式设置 tabstop、shiftwidth 等来实现这一点。这不是上面的Python文件的用途吗?为什么我必须在我的.vimrc
. 我如何从 Vim 插件中获取这些功能?
Current .vimrc:
当前 .vimrc:
syntax on
set hls
set expandtab
set textwidth=0
set tabstop=4
set softtabstop=4
set shiftwidth=4
set autoindent
set backspace=indent,eol,start
set incsearch
set ignorecase
set ruler
set wildmenu
set smarttab
filetype indent on
filetype on
filetype plugin on
采纳答案by abeyer
Setting tabstop, shiftwidth, etc... in your vimrc is correct. These set your global settings, as well as serve as parameters to the filetype-specific indentation support.
在你的 vimrc 中设置 tabstop、shiftwidth 等是正确的。这些设置您的全局设置,以及作为文件类型特定缩进支持的参数。
The language indentation plugins use these settings, but typically also set an indent expression (:he inde
) appropriate for the language. Thus the Python indenter should be automatically indenting after a block opening statement (def, class, for...), and dedenting after a closing one (return, pass, continue...) and doing so according to the ts,sw,... you have set.
语言缩进插件使用这些设置,但通常也会设置:he inde
适合语言的缩进表达式 ( )。因此,Python 缩进应该在块开始语句(def、class、for...)之后自动缩进,并在关闭语句(return、pass、continue...)之后自动缩进,并根据 ts、sw、 ……你已经设置了。
If you're still unsure if the plugin is loading for a buffer, simply do :filetype
to show the detection, plugin, and indent settings, and :set ft?
to see the detected type.
如果您仍然不确定插件是否正在为缓冲区加载,只需:filetype
显示检测、插件和缩进设置,并:set ft?
查看检测到的类型。
回答by chauncey
My understanding is that the python.vim file is just a syntax-highlighting file possibly, because Python files can be indented multiple ways. PEP8prescribes four spaces, but legacy files could be different including using tabs.
我的理解是 python.vim 文件可能只是一个语法高亮文件,因为 Python 文件可以以多种方式缩进。PEP8规定了四个空格,但旧文件可能会有所不同,包括使用制表符。
Some of our legacy Python files actually use two spaces per indent. So I leave Python indenting to Vim and configure it per file and per filetype. The following line in .vimrc gives me Python-specific settings which differ from say my xml, xhtml, and html (two spaces).
我们的一些遗留 Python 文件实际上每个缩进使用两个空格。因此,我将 Python 缩进留给 Vim 并按文件和文件类型对其进行配置。.vimrc 中的以下行为我提供了特定于 Python 的设置,这些设置不同于我的 xml、xhtml 和 html(两个空格)。
au FileType python setl shiftwidth=4 tabstop=4
You can also set specific settings by file with a modelinewhich is handy if you do have legacy files.
您还可以通过文件与一组特定的设置模式行,如果你有旧的文件,这是很方便的。
# vi: set tabstop=2 expandtab textwidth=70 filetype=python: