31

I am a newbie to python programming. I find that decreasing the indentation of a block of codes in python is quite annoying. For example, given the following code snippet

for i in range(density):
   if i < 5:
      x, y = rnd(0,shape[1]//2)*2, rnd(0,shape[0]//2)*2
      Z[y,x] = 1 
      ....
      ....

If I comment the if statement, I have to decrease the indentation for lines in the if block one by one, is there a way that I can do this by one key stroke as I increase the indentation of a block of codes by selecting them and press the TAB key? I guess this is environment dependent, so could you please provide solutions to do so in Eclipse+Pydev, VIM, and other common editors?

Christopher Creutzig
  • 8,656
  • 35
  • 45
sma
  • 877
  • 2
  • 9
  • 24

4 Answers4

68

In vim, you select the block and then press the < key.

In Eclipse you select it and then press SHIFT + TAB.

Every code editor worth its salt has a one-key way to indent and dedent blocks.

andr
  • 15,970
  • 10
  • 45
  • 59
Borealid
  • 95,191
  • 9
  • 106
  • 122
8

You could also replace the if statement with:

if True:  # if i < 5:

and leave everything else alone - no indent/dedent to undo later.

PaulMcG
  • 62,419
  • 16
  • 94
  • 130
3

Perhaps late for your case, but if useful for others:

SHIFT + TAB will do unindent for the selected text in Eclipse.

andr
  • 15,970
  • 10
  • 45
  • 59
sanja7s
  • 31
  • 4
1

Use Preferences-Pydev-Editor settings uncheck change tabs to spaces. It makes detent errors and your problem. And test other options like 4 space tab, 8 space tab and so on.

42n4
  • 1,292
  • 22
  • 26