Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim's tab length is different for .py files

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?

like image 480
Amxx Avatar asked Dec 10 '14 14:12

Amxx


People also ask

How long is tab in python?

Specify Tab SizeYou can specify a different tab size than the default size 8. If the length of the words is equal to the tab size then expandtabs() will add the number whitespaces after each word will be equal to tab size argument passed.

How do I set tab to two spaces in vim?

To convert tabs to spaces in the currently opened file in Vim, enter the Normal mode by pressing Esc key. Now use the retab command by pressing the ':' (colon) character and Vim will convert the existing tabs to spaces.


2 Answers

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.

like image 56
4 revs, 2 users 58% Avatar answered Sep 21 '22 15:09

4 revs, 2 users 58%


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.

like image 23
Kent Avatar answered Sep 20 '22 15:09

Kent