2

Possible Duplicate:
HOWTO: Fix Python Indentation

I have some code that is copy-pasted from older code, so the indentation is completely wrong now, and compiler gives errors.

Is there a way to automatically fix the indentation (not just swapping tabs with spaces)? I tried pyDev 2.7 (which claims to have auto-format) in Eclipse using Ctrl+Shift+F, but it didn't change anything.

Community
  • 1
  • 1
teddy teddy
  • 3,025
  • 6
  • 31
  • 48
  • Can you give an example of what is wrong with the indention? – Daniel Dec 10 '12 at 18:31
  • Depends on how broken it is, but chances are it requires *some* human thought. –  Dec 10 '12 at 18:32
  • first swap tabs with spaces to normalize ... then try and to autoformat ... but you will probably still have to manually fix some stuff ... does the old code run ? if so you should pretty much just be able to copy paste it /// – Joran Beasley Dec 10 '12 at 18:33
  • 1
    Also see [Indent python file with pydev in eclipse](http://stackoverflow.com/questions/7654267): “Just select the lines you want to indent and press Tab. If you want to unindent lines you have to press Shift+Tab” – James Waldby - jwpat7 Dec 10 '12 at 18:36
  • @jwpat7 - I don't believe this is a duplicate. The other question asks how to tidy up tab/space infelicities. None of the answers (as far as I can tell) actually work to fix broken indentation. This is what I want to know how to do and I can't see an answer. – Francis Davey Jun 10 '15 at 15:40
  • @Francis, maybe it's a duplicate, maybe it isn't. But the question gives too little info to effectively distinguish the questions. You could edit your question by providing examples of code that needs indentation fixes. Also indicate how each suggestion in the other question acts when you try it. (As mentioned in some answers, completely automatic Python indenting is impossible or impractical; you may need to manually indent some lines or regions. Note, in some python environments pressing tab repeatedly in the same line will cycle through all the language-legal positions for the line.) – James Waldby - jwpat7 Jun 11 '15 at 03:53
  • @jwpat7 - I don't have a question to edit. But I think someone (like me) looking for the answer to the question "what do I do with inconsistent indents" would find it useful to have a clear "there's no automated method" rather than having to read through lots of answers which coyly fail to say what their proposed solutions actually do. I wasted time on reindent autopep and so on before discovering that they didn't do what I wanted. A top/accepted answer which explains there are no tools that do this (if that is true) would be great. – Francis Davey Jun 11 '15 at 06:35
  • @Francis, sorry, thought question was yours. Anyhow, I agree it would be good to have a clear, correct, and accepted answer to this questions. A problem with "broken" indentation is that there may be several different possible indentations that are syntactically correct, which makes an automatic indenter difficult. – James Waldby - jwpat7 Jun 11 '15 at 15:11
  • @jwpat7 - Yes, thinking it through I realise why it can't be done programmatically. In my case some functions had 8 character indentation and others had 4 - that could be solved by a program but the general problem could not. In the end I laboriously went through and harmonised it all. – Francis Davey Jun 11 '15 at 16:11
  • I see one answer recommends using tab. Is there any other way to indent or unindent without using tab in PyDev? – sparrow Jun 12 '15 at 04:31

1 Answers1

1

use reindent

http://pypi.python.org/pypi/Reindent/0.1.0

pip install reindent
reindent <file>

You can also look at PythonTiddy

jassinm
  • 7,323
  • 3
  • 33
  • 42