Is there a quick way to decrease the indentation of multiple lines in Python?

Question:

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?

Asked By: sma

||

Answers:

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.

Answered By: Borealid

You could also replace the if statement with:

if True:  # if i < 5:

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

Answered By: PaulMcG

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.

Answered By: 42n4

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

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

Answered By: sanja7s