为什么Vim在Python文件中不服从我的expandtab? [英] Why does vim not obey my expandtab in python files?

查看:98
本文介绍了为什么Vim在Python文件中不服从我的expandtab?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

安装Vundle之后,我的vim不再遵循我的expandtab设置.我的标签页设置为2个空格,但现在在python文件中不再这样做.此行正在调用该问题:

After I installed Vundle, my vim no longer obeys the expandtab settings I had. My tabs were set to 2 spaces, but now in python files it no longer does that. The problem is being called by this line:

filetype plugin on

这行是做什么的(vundle要求)?另外,我该怎么做才能确保我的设置得到遵守?

What does this line do (It is required by vundle)? Also, what can I do to make sure my settings are obeyed?

谢谢!

VIMRC: pastebin.com/tGmfCi78

推荐答案

问题是您的设置被Vim的文件类型插件覆盖.问题在 ftplugin/python.vim :

The problem is that your settings are being overridden by a filetype plugin that's part of Vim. The issue is in ftplugin/python.vim:

" As suggested by PEP8.
setlocal expandtab shiftwidth=4 softtabstop=4 tabstop=8

python插件默认尝试将您的源代码设置为符合PEP8,因此它正在调整Tabstop.您将需要这些插件必须提供的一些功能,但是您可能需要设置自己的自动命令来修复所有您不喜欢的东西.

The python plugin attempts to setup your source code to be PEP8 compliant by default, so it's adjusting the tabstop. You'll want some of what these plugins have to offer, but you may need to setup your own autocommands to fixup anything you don't like.

执行此操作有两种方法.如果您有~/.vim文件夹,最简单的方法是添加文件~/.vim/after/ftplugin/python.vim:

There are two ways to go about doing this. If you have a ~/.vim folder, the easiest way is to add the file ~/.vim/after/ftplugin/python.vim:

" Here, you can set the setting directly, or call a command or function
" to help you.  We'll call a command, and then implement that command in
" your top-level vimrc to help keep things in one place.
SetupPython

在您的.vimrc中,添加:

function! SetupPython()
    " Here, you can have the final say on what is set.  So
    " fixup any settings you don't like.
    setlocal softtabstop=2
    setlocal tabstop=2
    setlocal shiftwidth=2
endfunction
command! -bar SetupPython call SetupPython()

后一点仅允许您在后文件中将函数调用为SetupPython而不是call SetupPython().

The latter bit just allows you to call the function as SetupPython rather than call SetupPython() in the after file.

另一种方法是将所有内容保留在.vimrc中,但是您可以使用VimEnter自动命令为python设置FileType自动命令来设置首选项.通过等到VimEnter被触发,所有其他插件将有时间设置其自动命令,因此您的插件将被添加到列表的末尾.这使您可以在python插件的FileType自动命令之后运行,并设置自己的设置.不过,这有点混乱,上面的after/机制是执行此操作的首选方式.

The other way, is to keep everything in your .vimrc, but you use the VimEnter autocommand to setup a FileType autocommand for python to set your preferences. By waiting until VimEnter is triggered, all the other plugins will have had time to setup their autocommands, so your's will be added to the end of the list. This allows you to run after the python plugin's FileType autocommand and set your own settings. This is a bit of a mess though, and the after/ mechanism above is the preferred way of doing this.

FWIW,我保存在SetupSource()函数中的许多常用设置可以从许多不同的FileType中调用.然后我要SetupPython()呼叫SetupSource().这有助于使特定功能保持简洁,并减少重复.如果有帮助,请在此处查看我的vimfile中的功能: https://github.com/jszakmeister /vimfiles/blob/master/vimrc#L5328

FWIW, many common settings I keep in a SetupSource() function to be called from a number of different FileTypes. Then I'd have SetupPython() call SetupSource(). This helps to keep the specific functions a little cleaner and reduce some duplication. If it helps, take a look at the functions in my vimfiles here: https://github.com/jszakmeister/vimfiles/blob/master/vimrc#L5328

这篇关于为什么Vim在Python文件中不服从我的expandtab?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆