Vim is automatically indenting Python code quite well.
For the following code everything works well:
1 while True:
2 |
If the cursor is at line 1 and the user hit o
in normal mode a new line is inserted and the indentation level is set to 1.
If the cursor is at line 2 and the user hit Ctrl-f
in insert mode the cursor moves to the first indentation level.
But for the following code the behavior is different:
1 while True:
2 print("start")
3 |
If the cursor is at line 2 and the user hit o
in normal mode a new line is inserted and the indentation level is set to 1.
If the cursor is at line 3 and the user hit Ctrl-f
in insert mode the cursor doesn't moves to the first indentation level.
I understand that the level of indentation 0 is also valid for that piece of code but most of the time you prefer the level 1 in such case.
Why the two behavior are different?
Is there a way to align them?
:
, it's not clear which indentation should be used—multiple answers are valid with different semantics! As for the controlling the logic, seecinkeys
and related options for C-f; see:help =
which refers to:help C-indenting
whenequalprg
is empty andindentexpr
is empty (for me in Python files, this last isGetPythonIndent(v:lnum)
); see:help o
with references to:help autoindent
,:help smartindent
/:help cindent
, and:help formatoptions
.