0

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?

3
  • Not sure, but when C-f doesn't work, try C-t/C-d
    – D. Ben Knoble
    Apr 26, 2022 at 13:41
  • Thanks Ben, C-t and C-d do work of course but requires you to indent more or less manually. It seems to me that the logic behind C-f is the same that the logic behind = which seems different from the logic behind o. I'm wondering why they are different and if someone have a version of = that corresponds to o. Apr 26, 2022 at 14:04
  • One issue of course with python is that, except for lines ending in :, it's not clear which indentation should be used—multiple answers are valid with different semantics! As for the controlling the logic, see cinkeys and related options for C-f; see :help = which refers to :help C-indenting when equalprg is empty and indentexpr is empty (for me in Python files, this last is GetPythonIndent(v:lnum)); see :help o with references to :help autoindent, :help smartindent/:help cindent, and :help formatoptions.
    – D. Ben Knoble
    Apr 26, 2022 at 14:10

0

Your Answer

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

Browse other questions tagged or ask your own question.