在_vimrc中 set noexpandtab python 不起效果

我ctm,今天配置不讓tab轉爲空格,在_vimrc中set noexpandtab 不起效果。python

set ts=4也不起效果。git

可是在命令行中其效果。github

 

我都不知道咋辦了。vim

問人說我有可能使用的不是那個目錄下的_vimrcc#

檢查了,沒問題。windows

後來在網上搜了半天都沒找到緣由。post

 

最後,機智的我用英文搜了一下,this

windows _vimrc set noexpandtab do not workspa

竟然找到答案了。插件

大意就是使用了python插件,裏面有一個什麼標準,會在加載_vimrc以後去加載,因此就把它覆蓋了。

就這麼簡單,下面是網址。

http://stackoverflow.com/questions/21073496/why-does-vim-not-obey-my-expandtab-in-python-files


11down voteaccepted

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

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.

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

In your .vimrc, add:

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

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

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 FileTypeautocommand 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, 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

相關文章
相關標籤/搜索