9

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Amxx
  • 3,020
  • 2
  • 24
  • 45
  • Do you mean to say that when you open an _existing_ Python file (which probably already has 4-spaces)? Vim won't automatically reindent to your preferred setting if that's what you mean. – Michael Berkowski Dec 10 '14 at 14:26
  • No, vim display `\t` as 4 spaces both when I open an existing file (with tabs for indentation) and when I create a new file with a `.py` extention – Amxx Dec 10 '14 at 14:27
  • Note, you can also do `let g:python_recommended_style = 0` to override the default `ftplugin/python.vim` (it only keeps default tabs) – Dylan Madisetti Mar 04 '21 at 19:32

2 Answers2

10

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.

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
Júda Ronén
  • 294
  • 1
  • 10
  • Why did you remove my edits? This seems like useful information to me? I originally made my own answer, but having all information in a single answer seemed better, because that reduces the amount of "noise" & duplicate information. – Martin Tournoij Dec 12 '14 at 20:16
  • I’m not sure about the etiquette here, but I think the edit is not fully in accord with the ‘how to edit’ section in the editing screen (► fix grammatical or spelling errors ► clarify meaning without changing it ► correct minor mistakes ► add related resources or links ► always respect the original author): it is more than just fixing grammatical or spelling errors, clarifying meaning without changing it or correcting minor mistakes; it do not add (external) resources or links; and it added things I didn’t write under my name without my prior agreement. – Júda Ronén Dec 12 '14 at 20:30
  • I will re-edit it in a moment; please say if the new wording is OK with you. – Júda Ronén Dec 12 '14 at 20:30
  • Well, sometimes a second answer just doesn't add enough information to warrant a new answer; your answer sits on top by virtue of being marked as accepted, my answer with the extra information somewhere below that ... It's generally accepted that an edit in these cases is fine, this makes the site a lot better for people who end up here through an internet search, and such... You didn't have to mark it as community wiki, btw ;-) – Martin Tournoij Dec 12 '14 at 20:47
  • This happens to work for me, but I would like to find the specific config that is taking hold and saying "My name is Python and I get to use special vim config props that I won't tell you about" – T.Woody Dec 01 '19 at 02:37
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.

Kent
  • 189,393
  • 32
  • 233
  • 301
  • that worked. The values where overiden in `/usr/share/vim/vim74/ftplugin/python.vim` as pointed out my Júda Ronén – Amxx Dec 10 '14 at 14:42