When I insert a line break inside a line of Python 3 code, the remaining part of the line is indented by two additional tab widths (2 time 4 spaces) in the next line.

# Before
example_function(argument_name_1, argument_name_2)

# After

How can I change this to only one additional indent?

# Desired

The script that controls Vim's indentation of Python files is located at $VIMRUNTIME/indent/python.vim. You can see the help file for it with :help ft-python-indent. From that, we learn there are a few global variables that can be used to control the indentation's behaviour:

The amount of indent can be set for the following situations. The examples given are the defaults. Note that the variables are set to an expression, so that you can change the value of 'shiftwidth' later.

Indent after an open paren:
  let g:pyindent_open_paren = 'shiftwidth() * 2'
Indent after a nested paren:
  let g:pyindent_nested_paren = 'shiftwidth()'
Indent for a continuation line:
  let g:pyindent_continue = 'shiftwidth() * 2'

With this in mind, to change the behaviour to the one you intend you can add the following to your .vimrc:

let g:pyindent_open_paren = 'shiftwidth()'

I'm assuming you are using the default vim python indentation file. That's where this behavior is set, and it's pretty easy to remove. My default indentation file is located at /usr/local/share/vim/vim74/indent/python.vim. Yours might be somewhere else, but you can search for it with this command (assuming you are on a unix environment)

find / -name "python.vim" -type f

The indentation file will be in some directory probably called indent.

Anyways, relevant part of the file looks like this.

  " When inside parenthesis: If at the first line below the parenthesis add
  " two 'shiftwidth', otherwise same as previous line.
  " i = (a
  "       + b
  "       + c)
  " NOTE: extra lines omitted for brevity
  return indent(plnum) + (exists("g:pyindent_nested_paren") ? eval(g:pyindent_nested_paren) : shiftwidth())
      return indent(plnum) + (exists("g:pyindent_open_paren") ? eval(g:pyindent_open_paren) : (shiftwidth() * 2))

We will change the last line (or preferably, comment it out!) to remove the *2.

return indent(plnum) + (exists("g:pyindent_open_paren") ? eval(g:pyindent_open_paren) : (shiftwidth()))

If you don't want to mess with default files (which you probably shouldn't), you should copy the file and put it in ~/.vim/indent/python.vim, and make your changes the the file in there. It will override the default file.

  • 5
    The base runtime scripts will always be at $VIMRUNTIME. You can view the script with :e $VIMRUNTIME/indent/python.vim – Tommy A Jul 13 '16 at 16:07
  • Whoa, neat! I didn't know about this. – DoYouEvenCodeBro Jul 13 '16 at 16:19
  • 1
    It is nice to see how you can override upstream scripts but the solution to set variable g:pyindent_open_paren is for sure the preferred one. – doak Feb 9 '20 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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