VIM and tab to space migration

Eli Stevens (WG.c) listsub at wickedgrey.com
Wed May 5 17:20:21 EDT 2004


Brian Quinlan <brian at sweetapp.com> wrote:
> Christophe Cavalaria wrote:
>
>> Try adding that line at the end of each modified file :
>>
>> # vim:sw=4:softtabstop=4:expandtab
>>
>> It tells vim all the commands it should automatically run when it
>> opens the file and it should get you the correct behaviour for 4
>> spaces no tab indentation.
>
> Thanks for the suggestion but modifying all my files is a pretty ugly
> solution. Not that I'm above using it if there is no better way :-)

Is it safe to assume you are working in a *nix environment?  If so, shell
scripts can be your friend (I use bash below).  The <ctrl-V><ctrl-I> below
is a literal tab character (bash specific, I think).  Note that this will
convert all tabs, not just those leading the line.

# Untested...
for file in `find . -type f -name "*.py"`
do
    cat $file | sed -e "s/<ctrl-V><ctrl-I>/    /g" > /tmp/tmp.py
    mv /tmp/tmp.py $file
done

Heck, if you wanted, you could even have the script add the vim: line for
you.  Of course, that would mean that running the below twice would start
adding duped, junk lines.

# Untested...
for file in `find . -type f -name "*.py"`
do
    cat $file | sed -e "s/<ctrl-V><ctrl-I>/    /g" > /tmp/tmp.py
    echo "# vim:sw=4:softtabstop=4:expandtab" >> /tmp/tmp.py
    mv /tmp/tmp.py $file
done

Managing the changeover so that multiple developers stay in synch without
creating merge conflicts is left as an exercise for the reader and his/her
version control software.  ;)

In a vain attempt to bring this back on-topic, how would people accomplish
the same in Python?  I can't imagine that it would be that hard, but I'm a
relative newbie.  :)

Eli

-- 
Give a man some mud, and he plays for a day.
Teach a man to mud, and he plays for a lifetime.
WickedGrey.com uses SpamBayes on incoming email:
               http://spambayes.sourceforge.net/
                                              --





More information about the Python-list mailing list