vim's tab length is different for .py files

2019-06-17 05:20发布

问题:

In my ~/.vimrc I set tab to me 2 spaces long

set shiftwidth=2
set tabstop=2

However when I open a .py file, tabs are 4 spaces long. I don't have specific configuration for python files. ~/.vim/after is empty and searching for py doesn't raise any suspect lines.

Have you ever experienced that? How to solve such a behaviour?

回答1:

It’s defined in the general Python filetype plugin file ($VIMRUNTIME/ftplugin/python.vim):

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

It should be so in order to conform with PEP 8.


@Carpetsmoker adds:

There is a discussion about this on the vim-dev@ list.

You can reset this using this in your ~/.vimrc; for example:

aug python
    " ftype/python.vim overwrites this
    au FileType python setlocal ts=4 sts=4 sw=4 noexpandtab
aug end

Or by adding config settings in $HOME/.vim/after.



回答2:

likely you have some plugin installed to ease your python editing, and those plugin re-set some vim options.

You can find out by:

  • open one py file, verify if tabstop/shiftwidth is 4
  • then run command: :verbose set ts and :verbose set sw

You can see where the options were set last time.