2

In my vimrc I have filetype indent plugin on

When I write python code, the indentation mostly looks good, but in lists it is too big:

def fun():
    return 'indent looks good'

my_list = [
        'indent too big'
        ]

Why is the indent size different in lists from the rest of code? How can I get the same size in lists?

I've tried set sts=4 sw=4 but the indentation in lists is still too big.

2 Answers 2

1

Found this section in the python indentation file (/usr/share/vim/vim82/indent/python.vim on my machine):

" When inside parenthesis: If at the first line below the parenthesis add
" two 'shiftwidth', otherwise same as previous line.
...
return indent(plnum) + (exists("g:pyindent_open_paren") ? eval(g:pyindent_open_paren) : (shiftwidth() * 2)) 
...

Changing the shiftwidth() * 2 to just shiftwidth() gives the indentation I want.

My guess is the reason for the double-shiftwidth is to satisfy the extra indentation suggested by PEP 8 here for arguments to functions with long names. But I'd rather have my lists indent correctly and manually fix argument lists when needed.

1
  • 1
    If your solution is addressing your problem maybe could you accept it. You can accept your own answer after t,o day. It allow the question to rest :-) Feb 12, 2023 at 17:45
1

With Vim 9.0 that behavior is now configurable.

If you want to disable the indentation of open parenthesis/bracket you can set

let g:pyindent_open_paren = shiftwidth()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.