views:

1377

answers:

6

I have a some .py files that use spaces for indentation, and I'd like to convert them to tabs.

I could easily hack together something using regexes, but I can think of several edge cases where this approach could fail. Is there a tool that does this by parsing the file and determining the indentation level the same way the python interpreter does?

+6  A: 

If there are not many files to convert, you can open them in vim, and use the :retab command.

See the vim documentation for more information.

Wimmel
+12  A: 

Since PEP-8 suggests that spaces a preferred over tabs, I'm curious why you're going from preferred (spaces) to not-preferred (tabs).

Wouldn't it be simpler to follow PEP-8 and leave them spaces?

S.Lott
By the way: Why is PEP-8 against tabs? I've always been fine with tabs, and my interpreter never complained...
Federico Ramponi
I would like to change them for consistency's sake.
Corey
@Federic Ramponi: Not against tabs. Recommend spaces. It's important to pick one, and spaces are easiest to get right. Some folks work okay with tabs, but spaces are guaranteed.
S.Lott
PEP-8 isn't "against" tabs, it's for consistency - and they chose spaces over tabs (I imagine the python-dev mailing list has a lot of discussion of why, such as http://mail.python.org/pipermail/python-list/2004-April/256325.html )
dbr
@Corey: Consistent -- yes. But consistent With what? Surely not other code formatted according to PEP-8.
S.Lott
In my opinion is better to be consistent with the existent code than try to change the whole code to your way (even though your way is the standard way)
igorgue
@igorgue: Since the question was how to change some files, it's not clear if all, most or a few files are being changed. "Consistent" with PEP-8 seems to be of slightly more value than consistent internally, but I don't know how much effort is involved.
S.Lott
As with most languages there exists different styles which are syntactically correct. PEP-8 defines the recommended standard and is, IMHO, a must-read for any Python coder. Consistent code is much nicer to work with.You'll get used to using 4 spaces and your editor/IDE should be able to help.
Walter
Not all follow PEP8 (use 4 spaces), but it's better for everyone if you do. That way you can easily cut/paste code from someone else into your code. If you don't eventuanlly you'll see, "IndentationError: unexpected indent" when you try to copy/paste someones code and you'll understand.
monkut
You shouldn't not have to "get used to" using 4-space tabs, your editor should hide this from you.. Press "tab" and it inserts 4 spaces, backspace should delete them just like tabs, and so on..
dbr
Agreed -- there's no "get used to" 4 spaces for tabs. All my editors hide this nuance from me.
S.Lott
My editor doesn't allow me to backspace 4 spaces in one hit which is unfortunate. NP++. The reason for spaces (I think I remember seeing) was for inconsistent rendering of tab across platforms. A space is always the same from platform to platform. Copy and paste destroys tabs quite regularly.
Josh Smeaton
+6  A: 

Python includes a script for the opposite (tabs to spaces). It's C:\Python24\Tools\Scripts\reindent.py for me

Greg
This is not available under Linux, even on py26.
Sorin Sbarnea
+1  A: 

In emacs, M-x tabify will convert spaces to tabs where possible. You'll probably want to set the tab-width variable appropriately.

I don't know if this addresses your concern that spaces be interpreted in the same way as the python interpreter, but you could always load up python-mode and use M-x indent-region.

Alastair
+3  A: 

:retab will swap tab with spaces, and :retab! will swap spaces with tab. 1 tab = 4 spaces, 4 spaces = 1 tab, depending on your tab setting.

Orjanp
+1, didn't know it worked both ways; it uses the `tabstop` setting, presumably?
Autopulated
+1  A: 

If you use Linux, you might also play around with unexpand:

Convert blanks in each FILE to tabs, writing to standard output. With no FILE, or when FILE is -, read standard input.

mzuther
You'll find a win32 build if you want.
Sorin Sbarnea