简体   繁体   English

Vim Python缩进无法正常工作?

[英]Vim Python indentation not working?

I have Vim 7 (enhanced) on CentOS 5, and it comes with all the usual Vim plugins/scripts ready to go. 我在CentOS 5上有Vim 7(增强版),它附带了所有常用的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. 我认为当打开以.py( vim file.py )结尾的文件时,它会自动加载这些插件,但我不确定是这种情况。 What I want is: 我想要的是:

Press TAB and receive four spaces. TAB键并接收四个空格。 Auto indent next line for suites, conditionals, etc. 套房,条件等的自动缩进下一行

I have this working by explicitly setting tabstop, shiftwidth, etc. in my .vimrc file. 我通过在.vimrc文件中明确设置tabstop,shiftwidth等来实现这一点。 Isn't this what the above Python files are for? 这不是上面的Python文件的用途吗? Why do I have to set these things in my .vimrc ? 为什么我必须在我的.vimrc设置这些东西? How do I get these features from the Vim plugins instead? 我如何从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

My understanding is that the python.vim file is just a syntax-highlighting file possibly, because Python files can be indented multiple ways. 我的理解是python.vim文件可能只是一个语法高亮文件,因为Python文件可以多种方式缩进。 PEP8 prescribes four spaces, but legacy files could be different including using tabs. PEP8规定了四个空格,但遗留文件可能不同,包括使用制表符。

Some of our legacy Python files actually use two spaces per indent. 我们的一些遗留Python文件实际上每个缩进使用两个空格。 So I leave Python indenting to Vim and configure it per file and per filetype. 所以我将Python缩进到Vim并按文件和每个文件类型配置它。 The following line in .vimrc gives me Python-specific settings which differ from say my xml, xhtml, and html (two spaces). .vimrc中的以下行给出了特定于Python的设置,这些设置与我的xml,xhtml和html(两个空格)不同。

au FileType python setl shiftwidth=4 tabstop=4

You can also set specific settings by file with a modeline which is handy if you do have legacy files. 你也可以用一组文件中的特定设置模式行 ,如果你有旧的文件,这是很方便的。

# vi: set tabstop=2 expandtab textwidth=70 filetype=python:

Setting tabstop, shiftwidth, etc... in your vimrc is correct. 在vimrc中设置tabstop,shiftwidth等是正确的。 These set your global settings, as well as serve as parameters to the filetype-specific indentation support. 这些设置您的全局设置,以及作为特定于文件类型的缩进支持的参数。

The language indentation plugins use these settings, but typically also set an indent expression ( :he inde ) appropriate for the language. 语言缩进插件使用这些设置,但通常还设置适合该语言的缩进表达式( :he inde )。 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. 因此,Python打印机应该在块打开语句(def,class,for ...)之后自动缩进,并且在关闭之后dedenting(返回,传递,继续......)并根据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? 如果您仍然不确定插件是否正在加载缓冲区,只需执行:filetype以显示检测,插件和缩进设置,然后:set ft? to see the detected type. 查看检测到的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM