Skip to content Skip to sidebar Skip to footer

Indent Python File (with Pydev) In Eclipse

I'm a newbie in eclipse. I want to indent all the lines of my code and formatting the open file by pressing a shortcut or something like that... I know the CTRL+SHIFT+F (as it actu

Solution 1:

If you want to change from 2 space to 4 space indentation (for instance), use "Source->Convert space to tab" with 2 spaces, then "Soruce->Convert tab to space" with 4 spaces.

Solution 2:

I ... don't think this question makes sense. Indentation is syntax in Python. It doesn't make sense to have your IDE auto-indent your code. If it's not indented properly already, it doesn't work, and the IDE can't know where your indentation blocks begin and end. Take, for example:

# Valid Code
for i in range(10):
  b =i

for j in range(b):
  c = j

# Also Valid Code.
for i in range(10):
  b =i

  for j in range(b):
    c = j

There's no possible way that the IDE can know which of those is the correct version, or what your intent is. If you're going to write Python code, you're going to have to learn to manage the indentation. There's no way to avoid it, and expecting the IDE to magically clean it up and still get the desired result out of it is pretty much impossible.

Further example:

# Valid Code.
outputData = []

for i inrange(100):
  outputData.append(str(i))

print''.join(outputData)

# Again, also valid code, wildly different behavior.
outputData = []

for i inrange(100):
  outputData.append(str(i))

  print''.join(outputData)

The first will produce a list of strings, then print the joined result to the console 1 time. The second will still produce a list of strings, but prints the cumulative joined result for each iteration of the loop - 100 print statements. The two are both 100% syntactically correct. There's no problem with them. Either of them could be what the developer wanted. An IDE can't "know" which is correct. It could, very easily incorrectly change the first version to the second version. Because the Language uses Indentation as Syntax, there is no way to configure an IDE to perform this kind of formatting for you.

Solution 3:

Although auto-indentation is not a feature of PyDev because of the language design you should be able to indent with a simple tab. Just select the lines you want to indent and press Tab. If you want to unindent lines you have to press Shift+Tab. Thats all.

Solution 4:

It is much easier:

  1. Select multiple lines
  2. Press Tab to indent (move right), Shift + Tab to unindent (move left) all selected lines.

Solution 5:

Indentation is syntactically significant; consider the difference between

for i in range(5):
    print i
print"done"

and

for i in range(5):
    print i
    print"done"

However, it certainly makes sense for the IDE to be able to normalize the existing indentation (e.g. apply a consistent number of spaces/tabs at each level).

Currently PyDev does not support such a feature; Pydev author Fabioz at one point expressed interest in adding it in the future and indicated that for now you can use the supplied reindent.py script to do it.

Post a Comment for "Indent Python File (with Pydev) In Eclipse"