indent python file (with pydev) in eclipse
Asked Answered
L

10

18

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 actually doesn't work in pydev!!) I've been searching for hours with no success. Is there any way to do that in eclipse. kind of like CTRL+K,D in visual studio, which formats and indents all the source code lines automatically?

Lieutenancy answered 4/10, 2011 at 21:29 Comment(0)
C
20

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 in range(100):
  outputData.append(str(i))

print ''.join(outputData)

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

for i in range(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.

Callaway answered 4/10, 2011 at 21:33 Comment(4)
This is correct. Python relies on indents for meaning, unlike C++ or Java. This means that Python cannot be "auto-indented", while others can.Knothole
Actually, if make a function call several lines long, indentation can be more loose.Marja
every ide should provide indentation or unidentation shortcuts and eclipse also have as said by @fivef belowPegram
Well not truly automatic indentation is possible, but selecting a code block and increasing the indentation of every line by 1 indentation or decreasing by 1 indentation should be.Rosemaryrosemond
P
25

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.

Pavlish answered 1/6, 2012 at 16:27 Comment(0)
C
20

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 in range(100):
  outputData.append(str(i))

print ''.join(outputData)

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

for i in range(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.

Callaway answered 4/10, 2011 at 21:33 Comment(4)
This is correct. Python relies on indents for meaning, unlike C++ or Java. This means that Python cannot be "auto-indented", while others can.Knothole
Actually, if make a function call several lines long, indentation can be more loose.Marja
every ide should provide indentation or unidentation shortcuts and eclipse also have as said by @fivef belowPegram
Well not truly automatic indentation is possible, but selecting a code block and increasing the indentation of every line by 1 indentation or decreasing by 1 indentation should be.Rosemaryrosemond
T
16

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.

Tench answered 24/5, 2012 at 13:53 Comment(1)
I did not know about "Shift+Tab". It will help surely :)Bora
C
14

It is much easier:

  1. Select multiple lines
  2. Press Tab to indent (move right), Shift + Tab to unindent (move left) all selected lines.
Crepuscular answered 19/8, 2014 at 12:32 Comment(0)
I
4

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.

Ingest answered 2/8, 2012 at 23:5 Comment(0)
T
2

Obviously this is only for Pydev, but I've worked out that you can get the very useful functions "Shift Right" and "Shift Left" (mapped by default to CTRL + ALT + . and CTRL + ALT + ,) to become useful by changing their keybindings to "Pydev Editor Scope" from "Pydev View". This effectively indents/dedents all lines that you've selected as much as you'd like

Trip answered 22/10, 2012 at 12:36 Comment(0)
P
1

I think that what you're looking for is some kind of shortcut in Eclipse/PyDev so that the selected code can be idented all at once. Just like when you create a new "if" or a "for" loop above a block of code and then need to rearrange the identation. The IDLE Editor has the "Ctrl + ]" shortcut that works exactly that way. It seems that the PyDev in Eclipse doesnt have something like that as far as I know.

Prognostic answered 21/5, 2012 at 14:13 Comment(0)
F
1

One can also select the lines, right click, then shift right / shift left

Firestone answered 11/3, 2015 at 7:33 Comment(0)
Z
1

It seems source formatting is still not available in PyDev.

For one off instances I found this web app does the job nicely.

http://pythoniter.appspot.com/

Zloty answered 29/4, 2015 at 15:30 Comment(0)
E
0

Like earlier said python requires to indent your code, so for other things like: space between variables passed as arguments to methods, etc., one can use ctrl+shift+f to format the code. This what is used for java, I tried for pydev and does some formatting.

Emotive answered 16/6, 2017 at 19:9 Comment(1)
First time user's opinion. If the interpreter reports / knows about "unexpected indentation" that implies it should be able to indent the text correctly, doesn't ? Am I asking to much from interpreter developer ?Pelerine

© 2022 - 2024 — McMap. All rights reserved.