2

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 a very tedious process to move to each line and change the indentation...is there a way to do auto indent or something?

For example:

def somefunction:
     x =5
     return x

If I want to add a control block

For example:

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 something to do this easily?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
vignesh
  • 963
  • 2
  • 8
  • 14

12 Answers12

5

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
  • 79,529
  • 41
  • 166
  • 227
3

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
  • 271,357
  • 39
  • 345
  • 578
2

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

Rex Logan
  • 23,002
  • 9
  • 33
  • 47
2

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

ctrl-c >
ctrl-c <
johannix
  • 27,528
  • 14
  • 37
  • 41
1

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

Jordan Parmer
  • 32,842
  • 27
  • 93
  • 118
1

[Funny ;-)] Dude, I told you that you would need one developer less if you had this new keyboard model Pythonic keyboard http://img22.imageshack.us/img22/7318/pythonkeyboard.jpg

Sergio
  • 4,261
  • 3
  • 29
  • 41
1

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

See also: Indent multiple lines quickly in vi

Community
  • 1
  • 1
e1i45
  • 1,339
  • 1
  • 13
  • 19
1

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
  • 2,431
  • 2
  • 16
  • 10
0

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


⌘ + [
or
⌘ + ]

To move the text in the appropriate direction.

Aaron
  • 566
  • 2
  • 4
  • 13
0

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
  • 566
  • 5
  • 11
  • 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 May 16 '09 at 10:14
0

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

S.Lott
  • 359,791
  • 75
  • 487
  • 757
0

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.

unmounted
  • 30,568
  • 14
  • 58
  • 61