Richard Jones' Log: So how about "-tt" on by default?

Wed, 26 Sep 2007

I've just hit my first-ever logic bug in a Python program that is directly attributable to tabs-vs-spaces.

I had code that looked like:

def tasty():
    spam = some_complex_code_resulting_in_True
    eggs = some_complex_code_resulting_in_False
    if eggs:
        print 'yum, eggs!'
    if spam:
        print 'mmm, spam!'

I was observing that I was never seeing "mmm, spam!" when I really ought to have been. That the code was also a Django site - which I have a rather unhappy relationship with to start with - meant that I was looking all over the damn place to try to figure what was going on.

Of course the real problem was that I was coding on a remote system in vim and I didn't realise that I'd not yet configured it to use "expandtabs", though the tab stop was set to 4 spaces. So the indentation of the "if spam" block was actually tabbed in, thus forming part of the "if eggs" block.

Questions: anyone know why "-tt" is not turned on by default? Is there any way to turn it on for something like mod_python?

Update: hey, look, the .vimrc does have expandtabs set on... it's just not working for some reason. Hrm.

Comment by Paul Fenwick on Wed, 26 Sep 2007

G'day Richard,

I believe that 'expandtabs' will expand NEW tabs that you insert. To convert existing tabs you'll want to 'retab' after setting expandtabs on.

:set et



Comment by Fredrik on Wed, 26 Sep 2007

"reindent -r" is your friend ;-)

(but seriously, shouldn't you do development on a local instance using your favourite tools, and deploy by doing "svn up" on the server?)

Comment by Richard Jones on Wed, 26 Sep 2007

@Paul: all the editing was done with expandtabs set - but through a "filetype" config line that apparently wasn't being used. I've now hard-coded expandtabs to always be on, and verified that it works :)

@Fredrik: yeah, in an ideal world I'd have a local dev instance :)

Comment by Derrick on Wed, 26 Sep 2007

-tt ... I didn't know that option existed. I would support turning it on by default.

Comment by Brett on Wed, 26 Sep 2007

Are you using the "official" Python vimrc file? It is set so that any .py file that has a leading tab shows up in red (on top of having expandtab set).

Comment by Simon Ward on Thu, 27 Sep 2007

I find setting �list� and �listchars� in Vim helpful for anything but files where tabs are actually used, like Makefiles: set list listchars=tab:��,trail:�,extends:>,precedes:<

One way to make sure -tt is used is to include it in the shebang: #!/usr/bin/python -tt

Unfortunately that doesn�t work when using: #!/usr/bin/env python

At the least, I think -t should be the default.

Comment by Toby on Thu, 27 Sep 2007

On the other hand, I find not using vi(m) very helpful.