views:

670

answers:

12

As i write code in python and suddenly feel like adding a new block in front of the code i have already written.... the indentation of the complete code is affected.. it is very tedious process to move to each line and change the indentation...is there any way to do auto indent or something...

eg:

def somefunction:
     x =5
     return x

if i want to add a contrl block

eg:

def somefunction:
     if True:
         x =5
         return x
     return 0

this small change of adding a control block took a lot of tab work....

is there a shortcut or sumthing to do this easily?

+3  A: 

In the Idle editor, you can just select the lines you want to indent and hit Tab.

I should note that this doesn't actually insert any tabs into your source, just spaces.

Mark Ransom
+1  A: 

In IDLE I just use ctrl+] and ctrl+[ on a block of code.

Rex Logan
+1  A: 

Use VI and never program the same again. :^)

j0rd4n
That is true on so many levels.
Bryan Oakley
…not all of them good! (Apologies to vi aficionados. :-)
Ben Blank
A: 

In TextMate, just highlight the lines you want to indent and use:


⌘ + [
or
⌘ + ]

To move the text in the appropriate direction.

Aaron
Edit: Sorry, this is only under Mac! My mistake. I forget there are other platforms some mornings...
Aaron
I'm pretty sure he's not using TextMate if he's not on a mac.
gnud
why vote this down?
Daniel
A: 

PyDev, which you can find at http://pydev.sourceforge.net/ has a "Code Formatter". It also has autoindent feature. It is a plugin for Eclipse which is freely available for Mac too.

Another option would be http://code.google.com/p/macvim/ if you are familiar or invest time for Vim, which has lots of autoindent features not just for Python.

But, do not forget that, in Python, indentation changes the meaning of the program unlike C family languages. For example for C or C#, a utility program can beautify the code according to the "{" and "}" symbols. But, in Python that would be ambiguous since a program can not format the following:

#Say we wrote the following and expect it to be formatted.
a = 1
for i in range(5):
print i
a = a + i
print a

Do you expect it to be

a = 1
for i in range(5):
    print i
a = a + i
print a #Will print 5

or

a = 1
for i in range(5):
    print i
    a = a + i
print a #Will print 11

which are two different snippets.

Caglar Toklu
i understand your point..but i just wanted a easy way to indent a block when i add a new block in front of the code...simple tabing itself solved the problem...
vignesh
+1  A: 
Sergio
+1  A: 

With emacs there's Python mode. In that mode you highlight and do:

ctrl-c >
ctrl-c <
+1  A: 

Vim: switch to visual mode, select the block, use > to indent (or < to unindent).

See also: http://stackoverflow.com/questions/235839/how-do-i-indent-multiple-lines-quickly-in-vi

e1i45
+1  A: 

If you are using vim there is a plugin specifically for this: Python_fn.vim

It provides useful python functions (and menu equivalents):

]t      -- Jump to beginning of block
]e      -- Jump to end of block
]v      -- Select (Visual Line Mode) block
]<      -- Shift block to left
]>      -- Shift block to right
]#      -- Comment selection
]u      -- Uncomment selection
]c      -- Select current/previous class
]d      -- Select current/previous function
]<up>   -- Jump to previous line with the same/lower indentation
]<down> -- Jump to next line with the same/lower indentation
redacted
+3  A: 

I don't know what wacky planets everyone is coming from, but in most editors that don't date back to the stone age, indenting blocks of code typically only requires that a block of text be selected and Tab be pressed. On the flip side, Shift+Tab usually UNdents the block.

This is true for Visual Studio, Notepad2, e, Textmate, Slickedit, #Develop, etc. etc. etc.

If you're not doing large multi-file projects, I strongly recommend Notepad2. Its a very lightweight, free, easy-to-use notepad replacement with just enough code-centric features (line numbers, indentation guides, code highlighting, etc.)

Soviut
shift-tab is usually unindent. ctrl-tab cycles through MDI windows (in MDI applications).
jmucchiello
You're right, corrected.
Soviut
I'm kind of disappointed in Idle though, Shift-Tab just indents more.
Mark Ransom
A: 

In Komodo the Tab and Shift Tab both work as expected to indent and unindent large blocks of code.

S.Lott
A: 

In vim, you can enter:

>>

to indent a line. If you enter:

5>>

you indent the 5 lines at and below the cursor. 5<< does the reverse.

bvmou