Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 566
sma Avatar asked Feb 18 '12 06:02

sma


2 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.

like image 115
Borealid Avatar answered Sep 28 '22 17:09

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.

like image 32
PaulMcG Avatar answered Sep 28 '22 17:09

PaulMcG