Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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.

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

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

ctrl-c >
ctrl-c <

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

[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

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

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

See also: Indent multiple lines quickly in vi

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


⌘ + [
or
⌘ + ]

To move the text in the appropriate direction.

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.

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top