Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim indentation levels with parentheses and brackets

Tags:

python

vim

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

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

I wanted to fix the indents in the file that contain this. For python I have softtabstop set to 4, shiftwidth set to 4 and expandtab set. When i did gg=G it resulted in the following:

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

There's two problems I have here. The first is that the closing brackets don't match up with the whitespace level of their opening brackets, they are indented to the same level as the code inside the brackets. This is seen with both the ( in the second line and its matching ) in the last line as well as the { in the third line and its matching } in the fifth line. I would like it so the closing brackets match up at the same level as their opening brackets.

My second problem is that the indentation after an opening parenthesis is double the indentation for a new block or even after an opening {. I'm thinking this might be part of the filetype indentation for Python, but I'm wondering how I can stop that so all the indents are 4 wide.

like image 573
Shaun Bouckaert Avatar asked May 12 '12 08:05

Shaun Bouckaert


People also ask

How do I open a parenthesis in Vim?

Most of the times you are writing code you not only want to open a parenthesis but open and close it. This is the solution I have found for that in Vim. Anyways, if you want to write a single opening parenthesis, just write ' (' twice.

Is there a way to show leading spaces in Vim?

This method is good because it uses Vim's built-in support for showing this kind of thing. However, this doesn't work for leading spaces. As pointed out by @Josh Petrie, there is a patch to work for spaces, but then you'd have to recompile Vim, which is beyond the scope of this answer.

Should I remove the {review}} line from the opening parenthesis?

If the tip contains good advice for current Vim, remove the { {review}} line. created November 4, 2001 · complexity basic · author Joachhim Hofmann · version 5.7 To automatically insert a closing parenthesis when typing an opening parenthesis you can insert the following simple mapping to your vimrc:

How do I automatically insert a closing parenthesis when typing parenthesis?

created November 4, 2001 · complexity basic · author Joachhim Hofmann · version 5.7 To automatically insert a closing parenthesis when typing an opening parenthesis you can insert the following simple mapping to your vimrc: This ends up with the cursor between the opening and the closing parenthesis in insert mode.


1 Answers

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,
    }),               
  )
like image 120
Vikas Avatar answered Nov 15 '22 05:11

Vikas