Function editing with Vim throws IndentError

Ethan Furman ethan at stoneleaf.us
Mon Aug 4 13:57:57 EDT 2008


Matimus wrote:
> On Jul 24, 9:32 pm, Lawrence D'Oliveiro <l... at geek-
> central.gen.new_zealand> wrote:
> 
>>In message
>><e23f51c1-7160-4aba-bea0-d624ec9a1... at w1g2000prk.googlegroups.com>, Matimus
>>wrote:
>>
>>
>>>On Jul 24, 2:54 am, Lawrence D'Oliveiro <l... at geek-
>>>central.gen.new_zealand> wrote:
>>>
>>>>In message
>>>><f558e635-aa40-4d54-bd1a-45e8463cd... at v26g2000prm.googlegroups.com>,
>>
>>>>Matimus wrote:
>>>>
>>>>>That isn't the standard. With that setup tabs will show up as 4
>>>>>spaces, and still confuse you.
>>
>>>>Why should that be confusing? The most common tab-stop setting is 4
>>>>columns.
>>
>>>A tab character is specified as 8 spaces.
>>
>>Specified by whom? The most common setting these days is 4 columns.
> 
> 
> All argument about specification aside, Python interprets a tab
> character as equivalent to 8 spaces. If you are treating tabs as
> equivalent to 4 spaces in your python code it will cause
> IndentationError exceptions to be raised.
> 
> If you set 'tabstop' to 4 in Vim all of the blocks of code indented
> using 4 spaces will be aligned with code indented with tabs. That is
> obviously problematic. Setting 'et' will fix the problem of inserting
> tabs, but does nothing to adjust the way tabs are displayed.
> 
> Vim has a feature 'softtabspace'/'sts' which is used to modify the way
> tabs are inserted without modifying the way they are displayed. If you
> are writing python code using Vim and you intend to indent using 4
> spaces (as recommended by pep8), the best practice I have found is to
> `set sw=4 ts=8 sts=4 et`.
> 
> Matt

When in doubt, try doing :retab as well -- it takes any tab characters 
in the file and converts them to spaces.  Very handy.

~Ethan~



More information about the Python-list mailing list