Skip to content Skip to sidebar Skip to footer

Vim Indentation Levels With Parentheses And Brackets

When I reindent a file using gg=G I noticed that indentation of a closing parenthesis or bracket doesn't match up with the line of the opening one. For example (with leading tabs s

Solution 1:

Use this indent script in vim to indent your python files. It does what is recommended in PEP-0008. The code you have posted, indented with the script gives me this:

if settings.DEBUG:
  urlpatterns += patterns('',
                          url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
                            'document_root': settings.MEDIA_ROOT,
                          }),
                         )

The recommendation for your second problem the recommendation is:

Continuation lines should align wrapped elements either vertically using Python's implicit line joining inside parentheses, brackets and braces, or using a hanging indent. When using a hanging indent the following considerations should be applied; there should be no arguments on the first line and further indentation should be used to clearly distinguish itself as a continuation line.

So indent script is doing the right thing.

Moreover the type of indentation you want, is recommended if you do not have any argument on the first line. So rearranging the code and using the indent script gives:

if settings.DEBUG:
  urlpatterns += patterns(
    '',
    url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
      'document_root': settings.MEDIA_ROOT,
    }),               
  )

Post a Comment for "Vim Indentation Levels With Parentheses And Brackets"