3

I have set tabstop and shiftwidth to 2 in my vimrc, but still when I go to a new line vim uses 4-space indents. I also get this warning if I save a file that uses 2 spaces for indentation:

E111 indentation is not a multiple of four [pep8]

How can I make vim use 2 spaces for Python? It seems to be ignoring my vimrc for Python.

I am using vim 7.2.

3
  • 4
    autocmd Filetype python setlocal ts=2 sw=2 expandtab Commented Jul 3, 2014 at 4:34
  • 9
    Don't do that. Commented Jul 3, 2014 at 4:36
  • @fabricator 's comment worked for me - but I also needed to add softtabstop=2 Commented Oct 19, 2022 at 12:09

2 Answers 2

8

This simply is not true. There is no mandate for whitespace in python and in fact Google's style guidelines for python recommend a shiftwidth of 2 spaces. 4 spaces is a PEP8 standard and is entirely a formatting preference. The ftplugin in vim defaults to use the PEP8 standard.

The cleanest way to override this is adding a python.vim file to the directory chain ~/after/ftplugin. Where ~ is the location of your vim configuration folder (usually .vim for unix and vimfiles for windows). You could then add your preferred configuration of formatting to this file.

The simplest example would be:
set shiftwidth=2
set tabstop=2

Sign up to request clarification or add additional context in comments.

2 Comments

This, but I needed (to overwrite OS settings?) set softtabstop=2 also.
The Google Style reference > github.com/google/styleguide
-1

Whitespace is an integral part of the Python language, and four spaces is the mandate. You could, however, use only shifts and have your editor read a shift as only two spaces, but I would not recommend this. This could easily cause problems with mixing spaces and tabs, which can cause a giant hassle.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.