为什么vim不遵守python文件中的expandtab? [英] Why does vim not obey my expandtab in python files?

查看:15
本文介绍了为什么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,因此它正在调整制表位.您可能需要这些插件提供的一些功能,但您可能需要设置自己的自动命令来修复您不喜欢的任何内容.

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()

后一点只允许您在 after 文件中将函数调用为 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天全站免登陆