Vim Tips Wiki


1,623pages on
this wiki
Add New Page
Add New Page Talk0

Use this page to discuss script 3003 indent/python: yet another Python indentation script

  • Add constructive comments, bug reports, or discuss improvements (see the guideline).
  • Do not document the script here (the author should do that on
  • This page may be out of date: check the script's page above, and its release notes.

Python indenting scriptEdit

This is my attempt at indenting Python to conform to PEP8. Based on the 7.2 Python indentation, with bits of script#974 hacked in.

Sample dataEdit

This is the Python code I tested with:

import line2, line3, line4, line5

class B:
    def foo():
        line1 = (line3({[a, b]
                        : 3,
                  ( "hello" ),

def bar(b, c):
    a = (b
         + c) - (c
                 + b)

class Rectangle(Blob):
    def __init__(self, width, height,
                 color='black', emphasis=None, highlight=0):
        if width == 0 and height == 0 and \
           color == 'red' and emphasis == 'strong' or \
           highlight > 100:
            raise ValueError("sorry, you lose")
        if width == 0 and height == 0 and (color == 'red' or
                                           emphasis is None):
            raise ValueError("I don't think so -- values are %s, %s" %
                             (width, height))
            Blob.__init__(self, width, height,
                          color, emphasis, highlight)


This is largely a huge improvement, but the indentation inside braces, brackets, etc. is a little aggressive.

For example:

 some_really_long_thing = {
    'foo': 'bar',<ENTER>

Hitting <ENTER> will always indent the cursor to the level of the "{", ignoring the previous lines indentation level.

I could change it to say a brace or parentheses only marks a new indent if it has text after it. If it is by itself at the end of the line, then the indentation for the next line should be unchanged.

Would that be an improvement?

-- David Moore 2010-10-24

That would be perfect, thanks David.

-- Alec Thomas 2010-10-24

Also on Fandom

Random Wiki